通过PHP中的CURL获取请求

时间:2016-02-05 03:11:30

标签: php curl

我有CURL脚本如下:

int

完整的字符串网址和消息,当我在Chrome网络浏览器上复制/过去时,远程PHP文件收到的很好。当PHP脚本发送的相同url +消息不起作用时。我想问题首先是远程域是HTTPS,第二个似乎是大括号和空格扰乱了CURL请求。我尝试了$url= 'https://www.test.com/test.php'; $msg=?p1={1250.feed}&p2={jt2221}&p3={1330}&p4={1234567890}&p5={2016-02-04 20:05:34}&p6={New York}; $url .= $msg; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); curl_close($ch); if (curl_errno($ch)) { echo 'Error: ' . curl_error($ch); } $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); var_dump($http_status); var_dump($result); 函数然后得到了错误404。在成功发送消息时,远程PHP返回urlencode($msg)作为ACK

1 个答案:

答案 0 :(得分:1)

如果您使用urlencode,则只需要对值进行编码,而不是整个查询字符串。执行此操作(并将查询数据保存在整齐数组中)的有效方法是使用http_build_query

$url= 'https://www.test.com/test.php?';

$data = array('p1' => '{1250.feed}',
              'p2' => '{jt2221}',
              'p3' => '{1330}',
              'p4' => '{1234567890}',
              'p5' => '{2016-02-04 20:05:34}',
              'p6' => '{New York}',);

$msg = http_build_query($data);

$url .= $msg;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
if (curl_errno($ch)) {
    echo 'Error: ' . curl_error($ch);
}
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

var_dump($http_status);
var_dump($result);