php 带有包含 GET url 的参数的 CURL 获取请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32596371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CURL Get Request with a parameter that contains a GET url
提问by Maxim Laco
I'm trying to make a cURL GET
to scrape a Facebook Graph object:
我正在尝试制作一个 cURLGET
来抓取 Facebook Graph 对象:
GET https://graph.facebook.com/?id=**OBJECT_URL**&scrape=true&method=post
In my case, OBJECT_URL
contains GET
parameters:
就我而言,OBJECT_URL
包含GET
参数:
https://www.example.com/og.php?a=b&c=d
For that reason I can't have it as a GET
parameter in file_get_contents()
or CURLOPT_URL
, as it'd turn out something like this:
出于这个原因,我不能将它作为or 中的GET
参数,因为它会变成这样:file_get_contents()
CURLOPT_URL
https://graph.facebook.com/?id=**https://www.example.com/og.php?a=b&c=d**&scrape=true&method=post
Is there a way to pass it as a GET
parameter in a way similar to CURLOPT_POSTFIELDS
?
有没有办法以GET
类似于的方式将其作为参数传递CURLOPT_POSTFIELDS
?
回答by Andrzej Budzanowski
You need to escape your parameters, the http_build_queryfunction will be useful:
您需要转义参数,http_build_query函数将很有用:
$query = http_build_query([
'id' => 'http://foo?a=1&b=2',
'scrape' => true,
'method' => 'post'
]);
$url = "https://graph.facebook.com/?".$query;
var_dump($url);
This will output:
这将输出:
https://graph.facebook.com/?id=http%3A%2F%2Ffoo%3Fa%3D1%26b%3D2&scrape=1&method=post