简体   繁体   English

通过PHP中的CURL进行GET请求

[英]GET request via CURL in PHP

I have CURL script as below: 我有如下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);

the full string url and message , when I copy/past on Chrome web browser the remote PHP file receives well. 完整的字符串url和message,当我在Chrome网络浏览器上复制/粘贴时,远程PHP文件接收良好。 When same url+message sent by PHP script not works. 当PHP脚本发送的相同url +消息不起作用时。 I guess the problem is first the remote domain is HTTPS , second seems curly brackets and space disturbs the CURL request.I tried urlencode($msg) function then got Error 404 . 我想问题是首先是远程域是HTTPS,其次是大括号和空格干扰了CURL请求。我尝试了urlencode($msg)函数,然后得到错误404。 On success sent message , remote PHP returns {"Code":null,"Msg":"."} as ACK 成功发送消息后,远程PHP返回{"Code":null,"Msg":"."}作为ACK。

If you are using urlencode , you'll just want to encode the values, not the whole query string. 如果您使用的是urlencode ,则只想编码值,而不是整个查询字符串。 An efficient way to do this (and keep your query data in neat arrays) is with http_build_query : 一种有效的方法(并将查询数据保持在整齐的数组中)是使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM