php:使用cURL获取url内容(json) [英] php: Get url content (json) with cURL

查看:972
本文介绍了php:使用cURL获取url内容(json)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要访问 https://graph.facebook.com/19165649929?fields=name (显然它也可以用http访问)与cURL以获取文件的内容,更具体:我需要名称(它的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);
?>

使用该代码,我得到一个空白页面!当我使用其他网页(如 http://www.google.com )时,它的作用就像一个魅力(我得到页面的内容)。我想Facebook是检查我不知道的东西...它可以是什么?如何使代码工作?谢谢!

解决方案

你在这里发布了吗?
php:使用cURL获取html源代码

但是在上面的线程中,我们发现你的问题beeing无法解析主机,这是解决方案:

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


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:

<?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.com it 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!

解决方案

did you double post this here? php: Get html source code with cURL

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

这篇关于php:使用cURL获取url内容(json)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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