将带有 -G 的 cURL 请求转换为 PHP
Translating cURL request with -G to PHP
我正在尝试使用 Facebook 营销 API,详见 this tutorial。
然而,我对如何将这个建议的 cURL 命令行请求转换成它的 PHP 等价物感到困惑:
curl -G \
-d 'access_token=<ACCESS_TOKEN>' \
https://graph.facebook.com/v2.5/<LEAD_ID>
我通常会这样做:
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$access_token);
$result = curl_exec($ch);
但是在尝试 运行 时会产生 'Unsupported post request' 错误。我想我误解了“-G”在命令行版本中的含义?
来自man curl:
-G, --get
When used, this option will make all data specified with -d, --data, --data-binary or --data-urlencode to be used in an HTTP GET request instead of the POST request that otherwise would be used. The data will be appended to the URL with a '?' separator.
没有cURL option flag in PHP与此直接对应。您可以使用
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
或
curl_setopt($ch, CURLOPT_HTTPGET, 'GET');
但这几乎没有必要:
CURLOPT_HTTPGET
TRUE to reset the HTTP request method to GET. Since GET is the default, this is only necessary if the request method has been changed.
您必须以不同方式指定请求参数:不要设置 CURLOPT_POSTFIELDS
,而是将它们作为查询字符串附加到 URL(如果需要,使用 urlencode or the equivalent curl_escape):
curl_setopt( $ch, CURLOPT_URL, $url . '?accesstoken='.urlencode('<ACCESS_TOKEN>');
我正在尝试使用 Facebook 营销 API,详见 this tutorial。
然而,我对如何将这个建议的 cURL 命令行请求转换成它的 PHP 等价物感到困惑:
curl -G \
-d 'access_token=<ACCESS_TOKEN>' \
https://graph.facebook.com/v2.5/<LEAD_ID>
我通常会这样做:
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$access_token);
$result = curl_exec($ch);
但是在尝试 运行 时会产生 'Unsupported post request' 错误。我想我误解了“-G”在命令行版本中的含义?
来自man curl:
-G, --get
When used, this option will make all data specified with -d, --data, --data-binary or --data-urlencode to be used in an HTTP GET request instead of the POST request that otherwise would be used. The data will be appended to the URL with a '?' separator.
没有cURL option flag in PHP与此直接对应。您可以使用
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
或
curl_setopt($ch, CURLOPT_HTTPGET, 'GET');
但这几乎没有必要:
CURLOPT_HTTPGET
TRUE to reset the HTTP request method to GET. Since GET is the default, this is only necessary if the request method has been changed.
您必须以不同方式指定请求参数:不要设置 CURLOPT_POSTFIELDS
,而是将它们作为查询字符串附加到 URL(如果需要,使用 urlencode or the equivalent curl_escape):
curl_setopt( $ch, CURLOPT_URL, $url . '?accesstoken='.urlencode('<ACCESS_TOKEN>');