2011-08-13 68 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