检索跨域数据php [英] Retrieving cross-domain data php

查看:213
本文介绍了检索跨域数据php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是从wikipedia检索一些数据,用ajax。我离开客户端脚本后,然后尝试检索一些随机内容。我尝试了fopen()和fread()方法,但它没有工作,然后我发现了一些文章,有互联网提供商使用代理的代码。因为这是我的case我尝试下面的代码,但它没有给任何响应。

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

好,所以我尝试建议代码,使用适当的代理值:

 <?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);
curl_close($ ch);

echo $ curl_scraped_pa​​ge;但是它给我这个错误:HTTP / 1.0 403禁止日期:星期一,2012年7月02日09:0 $ @



< 41:20 GMT服务器:Apache Content-Type:text / plain目标主机被禁止



我不明白为什么它不工作,问题。

解决方案

这不是一个真正的跨域问题,因为你从服务器而不是浏览器加载数据。 >

要通过代理从PHP加载网页 - 最好使用cURL(PHP http客户端: http://php.net/manual/zh-CN/book.curl.php )。



这是一个例子 - 它来自一个类似的问题(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_pa​​ge = curl_exec($ ch);
curl_close($ ch);

echo $ curl_scraped_pa​​ge;

如果您的代理需要身份验证 - 您可以设置$ proxyauth var ...


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.

解决方案

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...

这篇关于检索跨域数据php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆