php - PHP 中通过 CURL 获取请求

标签 php curl

我的 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/

相关文章:

php - 将电影详细信息从Google Play提取到网页

.net - PowerShell等效于curl HTTP POST的文件传输

php - xpath 中的 GET 语法

curl - 如何使用 curl 多部分表单数据从命令行发布数组字段?

php - 按计划部署 Azure 云服务

linux - 如何读取 CGI Bash 脚本中的 HTTP header 值

php - Twitter REST API – 使用 'Could not authenticate you' 参数时出现 'tweet_mode=extended' 错误

php - 如何使用php cURL发送Request Payload?

PHP $_SERVER ['HTTP_HOST' ] vs. $_SERVER ['SERVER_NAME' ],我是否正确理解了手册页?

PHP 允许使用旧的 session ID 而不更改它们,这使得 session 很容易被劫持。怎么修?