简体   繁体   中英

PHP Curl POST request not working but works fine in POSTMAN

Sending invoice details to main system from sub pos system. no errors either response in web browser. Request sent from https://example.com . But same code works fine in postman. Postman gives success response.

works fine in postman

code in php

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'urlhere',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
    "key":"asdasdsad", 
    "shopId":99999, 
    "SHOP_ID": "DUF SHOP", 
    "SHOP_NAME": "DUFF SUB", 
    "INVOICE_NUMBER": "26/111", 
    "TRANSACTION_DATE": "2020-08-25 7:31:20", 
    "TRANSACTION_TIME": "7:31:20",
    "TOTAL_AMOUNT_BEFORE_DISCOUNT": 150.00, 
    "DISCOUNT_AMOUNT": 50.00, 
    "DISCOUNT_TYPE": "NO",
    "TOTAL_AMOUNT_AFTER_DISCOUNT": 100.00, 
    "productDetails":[
        { 
        "INVOICE_NUMBER":"5210/D",
        "PRODUCT_NAME": "PRODUCT", 
        "PRODUCT_CATEGORY": "cat", 
        "PRODUCT_SUB_CATEGORY": "subCat",
        "BRAND_NAME": "Brand", 
        "QUANTITY":100, 
        "UNIT_PRICE": 250.00
        }],
    "currencyDetail":[
        {
        "INVOICE_NUMBER":"1",
        "PAYMENT_METHOD": "CASH",
        "CURRENCY": "USD", 
        "ACTUAL_PAYMENT_CURRENCY_TYPE":"USD",
        "amount":250.00       
        }
        ]
   }
   ',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: text/plain'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
?>

I use this site https://incarnate.github.io/curl-to-php/ sometimes. otherwise you can switch to json maybe. this example works for me.

    $handle = curl_init();
    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($handle, CURLOPT_POST, 1);
    curl_setopt($handle, CURLOPT_POSTFIELDS, "{\"text\": \"$newText\" , \"model_id\":\"en-fr\"}");
    curl_setopt($handle, CURLOPT_USERPWD, 'apikey' . ':' . $_ENV['API_KEY_TRANSLATE']);

    $headers[] = 'Content-Type: application/json';
    curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);

    $response = curl_exec($handle);
    $responseDecoded = json_decode($response, true);
    $responseCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);      
    curl_close($handle);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM