CURL使用包含GET URL的参数获取请求

Max*_*aco 17 php curl facebook-graph-api facebook-php-sdk

我正在尝试使用CURL GET来刮掉facebook打开的图形对象:

获取https://graph.facebook.com/?id= OBJECT_URL&scrape = true&method = post

在我的例子中,OBJECT_URL包含GET参数:

https://www.example.com/og.php?a=b&c=d

出于这个原因,我不能将它作为file_get_contents()或CURLOPT_URL中的GET参数,因为它会变成这样的:

https://graph.facebook.com/?id= https://www.example.com/og.php?a=b&c=d&scrape = true&method = post

有没有办法以类似于CURLOPT_POSTFIELDS的方式将其作为GET参数传递?

And*_*ski 35

你需要转义你的参数,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);
Run Code Online (Sandbox Code Playgroud)

这将输出:

https://graph.facebook.com/?id=http%3A%2F%2Ffoo%3Fa%3D1%26b%3D2&scrape=1&method=post
Run Code Online (Sandbox Code Playgroud)