0

I am using native php 5 (I know it sucks). I want to use curl post with some response

 { SUCCESS = 0,UNAUTHORIZED = 4,WRONG_PARAM = 5,DTIME_OLD = 6,WRONG_SIGN = 11,TOO_LONG = 12}

Actually i've tried on postman and it works with return 0 (SUCCESS).

But when I try on php 5 on localhost XAMPP, it always return empty reply from server.

I also check it on verbose cmd and it still no reply (img:https://i.stack.imgur.com/1zuIx.png).

Any idea, please?

Here is the code:

$path = "msisdn=087884327525&text=meong&sign=".$sign."&userid=19348&timestamp=".$date_fix;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, 
    array('accept: /User-Agent: python-requests/2.8.1',
          'accept-encoding: gzip, deflate', 
          'cache-control: no-cache',
          'connection: keep-alive', 
          'content-length: 119', 
          'Content-Type: application/x-www-form-urlencoded; charset=utf-8'
          // 'postman-token: 53c6e053-1692-010b-ffb9-b249ab94fca1'
        ));

$response = curl_exec($ch);
if ($response === false){
    print_r('Curl error: ' . curl_error($ch));
}else{
    echo "success";
}
curl_close($ch);
print_r($response);

ANSWER

I change the 'accept' header and separate it with 'user-agent'. And also, I move the timestamp path to prevent error '&times' into 'x'. I ran it on postman (works), then I copy the code from postman to my editor, and it works. Here is the code:

$curl = curl_init();

curl_setopt_array($curl, array(
  // CURLOPT_PORT => "9922",
  CURLOPT_URL => $host,
  CURLOPT_RETURNTRANSFER => true,
  // CURLOPT_ENCODING => "",
  // CURLOPT_MAXREDIRS => 10,
  // CURLOPT_TIMEOUT => 30,
  CURLOPT_SSL_VERIFYPEER => false ,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "timestamp=".$date_fix."&msisdn=087881257525&text=test%20USSD&sign=".$sign."&userid=19348",
  CURLOPT_HTTPHEADER => array(
    "accept: /",
    "accept-encoding: gzip, deflate",
    "cache-control: no-cache",
    "connection: keep-alive",
    "content-type: application/x-www-form-urlencoded",
    "postman-token: 95239f79-13c2-f64e-4fa0-d2498e5118c9",
    "user-agent: python-requests/2.8.1"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
Devin Ekadeni
  • 608
  • 5
  • 16
  • A dumb question but is curl enabled in php.ini used by the web server? Also check out this question: https://stackoverflow.com/questions/3018378/php-curl-error-empty-reply-from-server – Jacob Mulquin Nov 24 '17 at 05:16
  • Your Accept: header is invalid. Albeit this sounds more like a local firewall issue(?) – mario Nov 24 '17 at 05:56
  • @mulquin - i already did," extension=php_curl.dll ", this one right?. – Devin Ekadeni Nov 24 '17 at 06:42
  • @mario do you mean "/User-Agent: python-requests/2.8.1" this one is invalid value? But i use that value on postman and it works. Any idea which part of local firewall that block it? – Devin Ekadeni Nov 24 '17 at 06:42
  • Firewall is just a guess / you haven't told enough about your local setup. And should be `Accept: */*` and `User-Agent:` as separate header. Come to think of it, the `Content-Length:` seems inplausible for the given payload (and `&` would be wrong anyway). – mario Nov 24 '17 at 07:16
  • @mario on my local, i only uncomment "extension=php_curl.dll", is it enough? Ialready edit my header, i separate 'accept' and 'user-agent' as you suggest. And the '&amp' , i already remove it, and move the 'timestamp' to the front of the path. What should i do with my 'Content-Length'? Why is it inplausible? But, after i edit the path and the header, it still response the same result "empty reply" – Devin Ekadeni Nov 24 '17 at 07:45
  • @mario I finally did it, thank you for your help. I just copied the code from postman to my editor and it works. It seems like the problem was in the header as you said, thanks – Devin Ekadeni Nov 24 '17 at 08:11
  • In that case, please post your fixed code and some context in the answer box below. (Not sure if it's gonna help future visitors, but can't hurt.) – mario Nov 24 '17 at 08:24

0 Answers0