2011-12-05 40 views
0

可能重複:
get the value of an url response with curl如何在php中使用curl?

我已經stores.php現在我想看看使用curl這個頁面的輸出的PHP頁面名稱,我能做些什麼? 我的代碼是迄今stores.php頁

<?php 


include_once '../application/Boot.php'; 


if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    $body = @file_get_contents('php://input'); 
    $json = json_decode($body, true); 


    if (isset($json['version'])) { 
     $client_cache_version = @$json['version']; 

     $sql = $db->quoteInto("SELECT * FROM stores where version_modified > ". $client_cache_version); 
     $results = $db->fetchAll($sql); 


     $version_sql = $db->quoteInto("SELECT max(version_modified) as version FROM stores"); 
     $version_results = $db->fetchAll($version_sql); 

     $count = array(
      'count' => sizeof($results) 
     ); 

     array_push($results, $version_results['0']); 

     array_push($results, $count); 
     //ob_start("ob_gzhandler"); 

     header('HTTP/1.1 200 Stores list'); 
     echo json_encode($results); 
     exit; 

    }else { 


     header('HTTP/1.1 400 Bad Request'); 

     exit; 
    } 

}else{ 


    header('HTTP/1.1 400 Bad Request'); 
    exit; 
} 

?> 
+0

(OS ,...)你想使用curl來顯示腳本的響應? – scube

+0

重複? http://stackoverflow.com/q/2001897/508702 –

+0

@scube :) os是linux(ubuntu) – John

回答

2

如何使用捲曲顯示網頁的響應使用man curl

例如:

curl "http://www.stackoverflow.com" 
+0

:)確切地說,我想顯示一個網頁的響應,但在我的情況下顯示?我知道一些基本的命令,這個命令也是,但我怎麼能在我的情況下實現這樣的命令 – John

0
function getPage($url, $referer, $agent, $header, $timeout, $proxy="") 
    { 

     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_HEADER, $header); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     if($proxy != "") 
     { 
     curl_setopt($ch, CURLOPT_PROXY, $proxy); 
     curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0); 
     } 
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
     curl_setopt($ch, CURLOPT_REFERER, $referer); 
     curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
     curl_setopt($ch, CURLOPT_COOKIEJAR, realpath('cookies.txt')); 
     curl_setopt($ch, CURLOPT_COOKIEFILE, realpath('/cookies.txt')); 
     $result['EXE'] = curl_exec($ch); 
     $result['INF'] = curl_getinfo($ch); 
     $result['ERR'] = curl_error($ch); 

     curl_close($ch); 

     return $result; 
    } 

    $url = "www.targeturl.com"; 
    $referer = "http;//www.google.com"; 
    $agent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)'; 
    $header = 1; 
    $timeout = 15; 


    $result = getPage($url, $referer, $agent, $header, $timeout); 

    //$result["ERR"] contain errors if any one 
    //$result['EXE'] have the html of traget url you supplied in $url variable 
    //$result['info] have information. 

    you can use it like this 

    if(empty($result["ERR"])) // no error 
    { 
    echo $result['EXE']; //html of target url 
    } 
    else // errors 
    { 
     // do something on errors 
    } 

// $代理是可選 //如果你想通過代理來打開目標URL使用這樣

$proxy = "120.232.23.23:8080"; 
    $result = getPage($url, $referer, $agent, $header, $timeout,$proxy); 
在什麼樣的環境