【问题标题】:PHP Proxy Server and Calling JSON?PHP 代理服务器和调用 JSON?
【发布时间】:2011-10-26 12:41:27
【问题描述】:

好的,我正在尝试使用 PHP 代理访问一些 JSON,因为有人告诉我,当您不控制站点策略时,这是进行跨域访问的唯一方法。

下面是我尝试用作 php 代理的代码,由 stackoverflow 用户共享:

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

问题是当我将 $URL 替换为 http://www.nfl.com/liveupdate/scorestrip/ss.json 时,似乎什么也没发生。我也不确定如何使用这个 PHP 代理,因为我从来没有做过这种事情。

我想在单独的 php 文件中创建它,然后向该代码发送请求吗?我有点不知道到底该怎么做才能做到这一点,这样我就可以从上面的网站访问 json。

【问题讨论】:

    标签: php json cross-domain


    【解决方案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]; 
    

    这是我使用的脚本的略微修改版本,遗憾的是没有很好的文档记录。

    希望对你有帮助。

    【讨论】:

    • 这似乎输出:警告:无法修改标头信息 - 标头已由(输出开始于 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 ) 在第 88 行的 C:\inetpub\vhosts\allencoded.com\httpdocs\test.php 中警告:无法修改标头信息 - 标头已由(输出开始于 C:\inetpub\vhosts\allencoded.com\httpdocs\test .php:12) 在
    【解决方案2】:

    我建议使用 Ben Almans Simple PHP Proxy

    Simple PHP Proxy

    【讨论】:

      猜你喜欢
      • 2011-10-26
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2015-01-23
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      相关资源
      最近更新 更多