CURL Get Request with a parameter that contains a GET url

橙三吉。 提交于 2019-12-03 20:38:32

问题


I'm trying to make a cURL GET to scrape a Facebook Graph object:

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

In my case, OBJECT_URL contains GET parameters:

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:

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?


回答1:


You need to escape your parameters, the http_build_query function will be useful:

$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


来源:https://stackoverflow.com/questions/32596371/curl-get-request-with-a-parameter-that-contains-a-get-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!