0

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;
?>
  • 1
    A little tip: Postman has the option to generate the PHP code for you. On the right, look for an option called "Code" or denoted with a `>` symbol, then choose `PHP - cURL`. Then you can compare the differences. – El_Vanja Mar 18 '21 at 13:53
  • 1
    When you face issues like this, start by [debugging](https://stackoverflow.com/questions/3757071/php-debugging-curl) your cURL request to see what the server actually says. Without knowing what the server responds with, you're basically just guessing, which is quite sub optimal. – M. Eriksson Mar 18 '21 at 13:55
  • @El_Vanja no difference . I checked – Ashani Dahanayaka Mar 18 '21 at 14:09
  • Shouldn't `'Content-Type: text/plain'` be `'Content-Type: application/json'` since you're sending json? – M. Eriksson Mar 18 '21 at 15:13
  • @MagnusEriksson It must be Content-Type: text/plain and now I am getting error Failed to connect to 'domainofmainsystem' port 8005: Connection refused – Ashani Dahanayaka Mar 18 '21 at 16:43
  • So your server can't connect to the remote server. Unfortunately, we have no clue what systems you're running or how things are set up so you need to do some debugging to find out why it can't connect. There can by too many reasons for this. – M. Eriksson Mar 18 '21 at 20:12
  • @MagnusEriksson it is pos system and sending invoice details to main system. Can you explain me how to debug code please? – Ashani Dahanayaka Mar 19 '21 at 04:01
  • This isn't necessary the code that's the problem. It could be some server/network configuration. As mentioned, it could be any reason and since we don't know anything about your system or network, it's hard to be more specific. – M. Eriksson Mar 19 '21 at 07:38
  • solved. It is all beacause of browser's security. – Ashani Dahanayaka Mar 20 '21 at 16:47

1 Answers1

0

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);
user14189541
  • 17
  • 1
  • 7