确保在执行 GET 请求时将查询字符串放在 URL 末尾。
$qry_str = "?x=10&y=20";
$ch = curl_init();
// Set query data here with the URL
curl_setopt($ch, CURLOPT_URL, 'http://example.com/test.php' . $qry_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
$content = trim(curl_exec($ch));
curl_close($ch);
print $content;
使用 POST,您可以通过 CURLOPT_POSTFIELDS 选项传递数据
将其传递到 CURLOPT_URL 中。
$qry_str = "x=10&y=20";
curl_setopt($ch, CURLOPT_URL, 'http://example.com/test.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
// Set request method to POST
curl_setopt($ch, CURLOPT_POST, 1);
// Set query data here with CURLOPT_POSTFIELDS
curl_setopt($ch, CURLOPT_POSTFIELDS, $qry_str);
$content = trim(curl_exec($ch));
curl_close($ch);
print $content;
注意来自curl_setopt() docs http://php.net/curl_setopt for CURLOPT_HTTPGET
(强调):
[设置 CURLOPT_HTTPGET 等于]TRUE
to resetHTTP 请求方法为 GET。
由于 GET 是默认设置,因此仅当请求方法已更改时才需要这样做。