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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:04:20  来源:igfitidea点击:

CURL Get Request with a parameter that contains a GET url

phpfacebook-graph-apicurlfacebook-php-sdk

提问by Maxim Laco

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

我正在尝试制作一个 cURLGET来抓取 Facebook Graph 对象:

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

In my case, OBJECT_URLcontains GETparameters:

就我而言,OBJECT_URL包含GET参数:

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

For that reason I can't have it as a GETparameter 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 GETparameter 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