2011-07-06 81 views
10

我想从以下网址获得一些JSON数据:file_get_contents()和CURL的等价物?

$url = 'http://site.com/search.php?term=search term here'; 
$result = json_decode (file_get_contents($url)); 

但是客户端的主机服务商已经拿到了allow_url_fopen设置禁用,因此上面的代码不起作用。

上面几行的等效代码是什么?基本上,搜索字词需要通过$_GET提交到网址。

+0

我保护这是由于近_identical_答案的数量。人们,请考虑在添加另一个答案之前对现有答案进行投票表决,特别是如果现有答案涵盖了您想要覆盖的完全相同的结果。我们现在有五个几乎完全相同的如何使用curl获取URL的例子。 –

+1

@Tim Post认为a。到目前为止,回答问题的每个人都有> 10个代表,迄今为止的所有答案都是在提问后几分钟内给出的。因此,我认为保护是不必要的。我已经删除了我的答案(我不是第一个),但它似乎是唯一包含错误处理的答案,并且是撰写此答案时得票最高的答案。 – phihag

+0

@phihag我担心这会比它引发更多的意见(因为在头版上花费了相当多的时间)。绘制大量重复答案的问题也倾向于绘制大量标志,这主要是由于新用户试图赢得声望。我正在确保名单不会越来越多:)无论如何,现在不受保护。 –

回答

6

像这样:

$url = 'http://site.com/search.php?term=search term here'; 

$rCURL = curl_init(); 

curl_setopt($rCURL, CURLOPT_URL, $url); 
curl_setopt($rCURL, CURLOPT_HEADER, 0); 
curl_setopt($rCURL, CURLOPT_RETURNTRANSFER, 1); 

$aData = curl_exec($rCURL); 

curl_close($rCURL); 

$result = json_decode ($aData); 
4

所有你需要的是在这里。

http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curl

基本上,尝试这样的事情

function get_web_page($url) 
{ 
    $options = array(
     CURLOPT_RETURNTRANSFER => true,  // return web page 
     CURLOPT_HEADER   => false, // don't return headers 
     CURLOPT_FOLLOWLOCATION => true,  // follow redirects 
     CURLOPT_ENCODING  => "",  // handle all encodings 
     CURLOPT_USERAGENT  => "spider", // who am i 
     CURLOPT_AUTOREFERER => true,  // set referer on redirect 
     CURLOPT_CONNECTTIMEOUT => 120,  // timeout on connect 
     CURLOPT_TIMEOUT  => 120,  // timeout on response 
     CURLOPT_MAXREDIRS  => 10,  // stop after 10 redirects 
    ); 

    $ch  = curl_init($url); 
    curl_setopt_array($ch, $options); 
    $content = curl_exec($ch); 
    $err  = curl_errno($ch); 
    $errmsg = curl_error($ch); 
    $header = curl_getinfo($ch); 
    curl_close($ch); 

    $header['errno'] = $err; 
    $header['errmsg'] = $errmsg; 
    $header['content'] = json_decode($content); 
    return $header; 
} 
1

你检查出来是这样?

function file_get_contents_curl($url) { 
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser. 
    curl_setopt($ch, CURLOPT_URL, $url); 

    $data = curl_exec($ch); 
    curl_close($ch); 

    return $data; 
} 

谢谢。

1
// create a new cURL resource 
$ch = curl_init(); 

// set parameters 
$parameters = array(
    'foo'=>'bar', 
    'herp'=>'derp' 
); 

// add get to url 
$url = 'http://example.com/index.php' 
$url.= '?'.http_build_query($parameters, null, '&'); 

// set URL and other appropriate options 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return the content 

// execute 
$data = curl_exec($ch); 

然后,您应该在$数据文件的内容,你可能会想要做一些错误检查,但你可以找到如何做到这一点的php.net

5
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$jsonData = curl_exec($ch); 
if ($jsonData === false) { 
    throw new Exception('Can not download URL'); 
} 
curl_close($ch); 
$result = json_decode($jsonData);