我的 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);
完整的字符串 url 和消息,当我在 Chrome Web 浏览器上复制/粘贴时,远程 PHP 文件接收良好。当 PHP 脚本发送的相同 url+消息不起作用时。我想问题首先是远程域是 HTTPS ,其次似乎是大括号和空格干扰了 CURL 请求。我尝试了 urlencode($msg) 函数,然后得到了 Error 404 。成功发送消息后,远程 PHP 返回 {"Code":null,"Msg":"."}
作为 ACK
最佳答案
如果您使用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);
关于php - PHP 中通过 CURL 获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35215672/