2011-08-13 37 views
0

好吧我試圖訪問一些使用PHP代理的JSON,因爲我被告知是在不控制網站策略時進行跨域訪問的唯一方法。PHP代理服務器和調用JSON?

這裏是下面的代碼,我想通過一個老鄉計算器用戶共享一個PHP代理使用方法:

function curl_download($Url){ 

    // is cURL installed yet? 
    if (!function_exists('curl_init')){ 
     die('Sorry cURL is not installed!'); 
    } 

    // OK cool - then let's create a new cURL resource handle 
    $ch = curl_init(); 

    // Now set some options (most are optional) 

    // Set URL to download 
    curl_setopt($ch, CURLOPT_URL, $Url); 

    // Set a referer 
    curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm"); 

    // User agent 
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); 

    // Include header in result? (0 = yes, 1 = no) 
    curl_setopt($ch, CURLOPT_HEADER, 0); 

    // Should cURL return or print out the data? (true = return, false = print) 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    // Timeout in seconds 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 

    // Download the given URL, and return output 
    $output = curl_exec($ch); 

    // Close the cURL resource, and free system resources 
    curl_close($ch); 

    return $output; 
} 

問題是,當我更換http://www.nfl.com/liveupdate/scorestrip/ss.json沒什麼$ URL似乎發生。我不確定如何使用這個PHP代理,雖然我從來沒有做過這種類型的事情。

我想創建一個單獨的PHP文件,然後發送請求到此代碼?我真的很想在這裏做什麼,以便我可以從上面的網站訪問json。

回答

1

我想創建一個單獨的PHP文件,然後發送請求到此代碼?

是的。上面的代碼應該重新發送您的JS請求到另一個域上的遠程服務。這是什麼技巧 - 啓用來自JS的跨域POST請求。

<?php 

$server_url = "http://example.com/"; 

$options = array 
(
    CURLOPT_HEADER   => true, 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_FOLLOWLOCATION => true, 
    CURLOPT_TIMEOUT  => 60, 
    CURLOPT_CONNECTTIMEOUT => 0, 
    CURLOPT_HTTPGET  => 1 
); 

$service = $_GET["service"]; 

$request_headers = Array(); 
foreach($_SERVER as $i=>$val) { 
     if (strpos($i, 'HTTP_') === 0) { 
       $name = str_replace(array('HTTP_', '_'), array('', '-'), $i); 
       if ($name != 'HOST') 
       { 
        $request_headers[] = "{$name}: {$val}"; 
       } 
     } 
} 

$options[CURLOPT_HTTPHEADER] = $request_headers; 

switch (strtolower($_SERVER["REQUEST_METHOD"])) 
{ 

    case "post": 
     $options[CURLOPT_POST] = true; 
     $url = "{$server_url}/services/".$service; 

     $options[CURLOPT_POSTFIELDS] = file_get_contents("php://input"); 

     break; 
    case "get": 

     unset($_GET["service"]); 

     $querystring = ""; 
     $first = true; 
     foreach ($_GET as $key => $val) 
     { 
      if (!$first) $querystring .= "&"; 
      $querystring .= $key."=".$val; 
      $first = false; 
     } 

     $url = "{$server_url}/services/".$service."?".$querystring; 

     break; 
    default: 
     throw new Exception("Unsupported request method."); 
     break; 

} 

$options[CURLOPT_URL] = $url; 

$curl_handle = curl_init(); 

curl_setopt_array($curl_handle,$options); 
$server_output = curl_exec($curl_handle); 
curl_close($curl_handle); 

$response = explode("\r\n\r\n",$server_output); 
$headers = explode("\r\n",$response[0]); 

foreach ($headers as $header) 
{ 
    if (!preg_match(';^transfer-encoding:;ui', Trim($header)) ) 
    { 
     header($header); 
    } 
} 

echo $response[1]; 

這是我使用的腳本的稍微修改版本,遺憾的是沒有很好的記錄。

希望它有幫助。

+0

這似乎輸出:警告:不能更改頭信息 - 頭已經發出(輸出開始在C:\的Inetpub \虛擬主機\ allencoded。 com \ httpdocs \ test.php:12)在第88行的C:\ inetpub \ vhosts \ allencoded.com \ httpdocs \ test.php中警告:無法修改標頭信息 - 已經發送的標頭(輸出開始於C:\ inetpub \ vhosts \ allencoded.com \ httpdocs \ test.php:12)在C:\ inetpub \ vhosts \ allencoded.com \ httpdocs \ test.php在線88警告:無法修改標題信息 - 已經發送的標題(輸出從C開始:\ inetpub \ vhosts \ allencoded.com \ httpdocs \ test.php:12)in – allencoded