php:使用 cURL 获取 url 内容 (json)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3592802/
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
php: Get url content (json) with cURL
提问by John Paneth
I want to access https://graph.facebook.com/19165649929?fields=name(obviously it's also accessable with "http") with cURL to get the file's content, more specific: I need the "name" (it's json). Since allow_url_fopen is disabled on my webserver, I can't use get_file_contents! So I tried it this way:
我想使用 cURL访问https://graph.facebook.com/19165649929?fields=name(显然它也可以通过“http”访问)以获取文件的内容,更具体的:我需要“名称”(它是 json) . 由于我的网络服务器上禁用了 allow_url_fopen,我无法使用 get_file_contents!所以我这样试过:
<?php
$page = 'http://graph.facebook.com/19165649929?fields=name';
$ch = curl_init();
//$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
//curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>
With that code I get a blank page! When I use another page, like http://www.google.comit works like a charm (I get the page's content). I guess facebook is checking something I don't know... What can it be? How can I make the code work? Thanks!
使用该代码,我得到一个空白页面!当我使用另一个页面时,比如http://www.google.com,它就像一个魅力(我得到了页面的内容)。我猜 facebook 正在检查一些我不知道的东西......它会是什么?我怎样才能使代码工作?谢谢!
回答by The Surrican
did you double post this here? php: Get html source code with cURL
你在这里双重发布了吗? php:使用 cURL 获取 html 源代码
however in the thread above we found your problem beeing unable to resolve the host and this was the solution:
但是在上面的线程中,我们发现您的问题无法解析主机,这是解决方案:
//$url = "https://graph.facebook.com/19165649929?fields=name";
$url = "https://66.220.146.224/19165649929?fields=name";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: graph.facebook.com'));
$output = curl_exec($ch);
curl_close($ch);
回答by svens
Note that the Facebook Graph API requires authentication before you can view any of these pages.
请注意,Facebook Graph API 需要身份验证才能查看这些页面中的任何一个。
You basically got two options for this. Either you login as an application (you've registered before) or as a user. See the api documentationto find out how this works.
你基本上有两个选择。您可以作为应用程序(您之前注册过)或作为用户登录。请参阅api 文档以了解其工作原理。
My recommendation for you is to use the official PHP-SDK. You'll find it here. It does all the session and cURL magic for you and is very easy to use. Take the examples which are included in the package and start to experiment.
我给你的建议是使用官方的 PHP-SDK。你会在这里找到它。它为您完成所有会话和 cURL 魔术,并且非常易于使用。以软件包中包含的示例开始实验。
Good luck.
祝你好运。