通过CURL在PHP中的GET请求 [英] GET request via CURL in PHP
问题描述
我有CURL脚本如下:
I have CURL script as below:
$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 +消息发送PHP脚本不工作。我想问题是第一个远程域是HTTPS,第二个似乎大括号和空间扰乱CURL请求。我尝试 urlencode($ msg)
函数然后得到错误404。在成功发送消息后,远程PHP返回 {Code:null,Msg:。}
as ACK
the full string url and message , when I copy/past on Chrome web browser the remote PHP file receives well. When same url+message sent by PHP script not works. 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 . On success sent message , remote PHP returns {"Code":null,"Msg":"."}
as ACK
推荐答案
如果你使用 urlencode
,你只需要对值进行编码,而不是整个查询字符串。执行此操作(并保持查询数据在整齐数组中)的有效方法是使用 http_build_query
:
If you are using urlencode
, you'll just want to encode the values, not the whole query string. An efficient way to do this (and keep your query data in neat arrays) is with 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);
这篇关于通过CURL在PHP中的GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!