CURL获取带有参数的请求,该参数包含GET URL [英] CURL Get Request with a parameter that contains a GET url

查看:449
本文介绍了CURL获取带有参数的请求,该参数包含GET URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个cURL GET来抓取Facebook Graph对象:

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

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

In my case, OBJECT_URL contains GET parameters:

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

由于这个原因,我不能将其作为file_get_contents()CURLOPT_URL中的GET参数,因为结果是这样的:

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

是否有一种类似于CURLOPT_POSTFIELDS的方式将其作为GET参数传递?

Is there a way to pass it as a GET parameter in a way similar to CURLOPT_POSTFIELDS?

推荐答案

您需要转义

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);

这将输出:

https://graph.facebook.com/?id=http%3A%2F%2Ffoo%3Fa%3D1%26b%3D2&scrape=1&method=post

这篇关于CURL获取带有参数的请求,该参数包含GET URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆