0

I'm trying to call 3rd party API with POST method using curl. But I'm not getting response instead I got this random character when I dump the response

enter image description here

$data=array(
    "referenceId"=>$orders->reference,
    "currency"=>"IDR","amount"=>intval($total_bayar),
    "callbackUrl"=>"https://www.some.url",
    "paymentResultUrl"=>"https://www.some.url",
    "paymentCancelUrl"=>"https://www.some.url",
    "merchantReferenceId"=>$orders->reference,
    "customerInfo"=>array(
        "mobileNumber"=>$customerAddress->phone,
        "fullName"=>$customerAddress->firstname.''.$customerAddress->lastname
    ),
    "shippingAddress"=>array(
        "countryCode"=>"ID",
        "lines"=>[$customerAddress->address1],
        "postCode"=>$customerAddress->postcode
    ),
    "billingAddress"=>array(
        "countryCode"=>"ID",
        "lines"=>[$customerAddress->address1],
        "postCode"=>$customerAddress->postcode
    ),
    "taxAmount"=>0,
    "items"=>$itemsDetail
);

$postdata=json_encode($data);
$headers=array();
$headers[]='Content-type:application/json';
$headers[]='Connection:keep-alive';
$headers[]='Accept-Encoding:gzip,deflate,br';
$headers[]='Authorization:Basic'.base64_encode('some_key:some_pass');

$service_url='https://www.some.url';
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$service_url);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,0);
curl_setopt($ch,CURLOPT_TIMEOUT,400);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postdata);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
$curl_response=curl_exec($ch);
$httpCode=curl_getinfo($ch,CURLINFO_HTTP_CODE);

if($curl_response===false)
    $curl_response=curl_error($ch);

curl_close($ch);

print_r($curl_response);
die();

I tried to debug using the chrome network tab, but my API call doesn't show up in my network tab. Any help would be appreciated, thank you

Michael
  • 55
  • 2
  • 9
  • I don't know what you expect us to do with this. You're setting up a cURL call to an API that you've removed. There's no way to know what you should be sending, or what you might expect in return. – Tangentially Perpendicular Apr 04 '21 at 04:55
  • `Accept-Encoding:gzip,deflate,br` can you explain what do you expect in reponse? Json or zip content? – nice_dev Apr 04 '21 at 04:58
  • @nice_dev hi, i'm expecting json as response – Michael Apr 04 '21 at 05:11
  • 2
    @Michael Could you remove that header and replace it with `Accept-type:application/json` and check? – nice_dev Apr 04 '21 at 05:12
  • As noted elsewhere, either remove the `Accept-Encoding` header, or try the solutions from https://stackoverflow.com/questions/310650/decode-gzipped-web-page-retrieved-via-curl-in-php – msbit Apr 04 '21 at 09:42

1 Answers1

1

try to remove this $headers[]='Accept-Encoding:gzip,deflate,br';

Raf Lii
  • 28
  • 5