dongli1920
2012-07-01 12:21
浏览 54
已采纳

检索跨域数据php

What I'd like to do is retrieve some data from wikipedia, with ajax. I left the client-side scripting for afterwards and tried retrieving some random content. I tried with the fopen() and fread() methods but it didn't work, and then I came around some article that had the code for internet-providers that used proxies. Since it's my case I tried the code below but it didn't give any response.

<?php
$opts = array('http' => array('proxy' => 'tcp://10.10.10.101:8080', 'request_fulluri' => true));
$context = stream_context_create ($opts);
$data = file_get_contents('http://www.php.net', false, $context);   
echo $data;
?>

Ok so I tried the code suggested, with the proper proxy values:

<?php


$url = 'http://www.php.net';
$proxy = '10.10.10.101:8080';
//$proxyauth = 'user:password';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);

echo $curl_scraped_page;

But it gives me this error: HTTP/1.0 403 Forbidden Date: Mon, 02 Jul 2012 09:41:20 GMT Server: Apache Content-Type: text/plain Destination host forbidden

I don't get why it doesn't work, and how I could solve the problem.

图片转代码服务由CSDN问答提供 功能建议

我想做的是从维基百科中使用ajax检索一些数据。 之后我离开了客户端脚本并尝试检索一些随机内容。 我尝试使用fopen()和fread()方法,但它没有用,然后我找到了一些文章,其中包含使用代理的互联网提供商的代码。 因为这是我的情况,我尝试了下面的代码,但它没有给出任何响应。

 &lt;?php 
 $ opts = array('http'=&gt; array  ('proxy'=&gt;'tcp://10.10.10.101:8080','request_fulluri'=&gt; true)); 
 $ context = stream_context_create($ opts); 
 $ data = file_get_contents('http:  //www.php.net',false,$ context);  
echo $ data; 
?&gt; 
   
 
 

好的,所以我尝试使用正确的代理值建议的代码: < pre> &lt;?php $ url ='http://www.php.net'; $ proxy = '10 .10.10.101:8080'; // $ proxyauth = 'user:password'; $ ch = curl_init(); curl_setopt($ ch,CURLOPT_URL,$ url); curl_setopt($ ch,CURLOPT_PROXY,$ proxy); // curl_setopt($ ch, CURLOPT_PROXYUSERPWD,$ proxyauth); curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,1); curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ ch,CURLOPT_HEADER,1); $ curl_scraped_pa​​ge = curl_exec($ ch); ncurl_close($ ch); echo $ curl_scraped_pa​​ge;

但它给了我这个错误:HTTP / 1.0 403禁止日期:星期一,2012年7月2日 09:41:20 GMT服务器:Apache Content-Type:text / plain目的地主机禁止

我不明白为什么它不起作用,以及我如何解决问题。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongzhiqi0332 2012-07-01 12:37
    已采纳

    it's not really a cross domain problem because you are loading the data from the server not the browser.

    To load a web page from PHP via a proxy - it's best to use cURL (a PHP http client: http://php.net/manual/en/book.curl.php).

    Here is an example - it is taken from a similar question (http://stackoverflow.com/questions/5211887/how-to-use-curl-via-a-proxy):

    <?php
    
    
    $url = 'http://www.php.net';
    $proxy = '10.10.10.101:8080';
    //$proxyauth = 'user:password';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_PROXY, $proxy);
    //curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $curl_scraped_page = curl_exec($ch);
    curl_close($ch);
    
    echo $curl_scraped_page;
    

    If your proxy needs authentication - you can set the $proxyauth var...

    打赏 评论
  • dongqiao8417 2012-07-02 11:51

    I just tested your code - simply using my own proxy address - and it works.

    <?php
        $url = 'http://www.php.net';
        $proxy = '192.168.4.200:3128';
        //$proxyauth = 'user:password';
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
        //curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        $curl_scraped_page = curl_exec($ch);
        curl_close($ch);
    
        echo $curl_scraped_page;
    ?>
    

    So, what you're seeing is probably the proxy itself, which does not allow (some - or all external?) sites to be reached. Maybe all you need is authenticating with the proxy.

    This probably means that you won't be able to do this via get_contents, curl, fsockopen, or any other way until you've cleared this with the network administrators.

    打赏 评论

相关推荐 更多相似问题