且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过CURL在PHP中的GET请求

更新时间:2023-02-24 12:38:25

如果你使用 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);