CURL响应来自服务器的空回复

时间:2017-11-24 05:08:58

标签: php curl post localhost

我使用的是原生的php 5(我知道它很糟糕)。我想使用curl post和一些回复

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

实际上我已经尝试过postman,它适用于返回0(SUCCESS)。

但是当我在localhost XAMPP 上尝试使用php 5时,它总是从服务器返回空回复。

我也在verbose cmd上检查它,但仍然没有回复(img:https://i.stack.imgur.com/1zuIx.png)。

请问,好吗?

以下是代码:

$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

我更改'accept'标头并将其与'user-agent'分开。而且,我移动时间戳路径以防止错误'& times'变成'x'。我在postman(作品)上运行它,然后我将代码从postman复制到我的编辑器,它可以工作。 这是代码:

$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;
}

0 个答案:

没有答案