获取url内容PHP [英] get url content PHP

查看:118
本文介绍了获取url内容PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想把一个URL的内容放在一个字符串中,并处理它。但是,我有一个问题。



我收到此错误:

 警告:file_get_contents //www.findchips.com/avail?part=74ls244)[function.file-get-content]:无法打开流:重定向限制已达到,

我听说这是由于页面保护和标题,cookie和东西。
我如何覆盖它?



我也尝试过替代品,例如fread和fopen,但我想我不知道该怎么做。


$ b

解决方案

1)本地最简单的方法 p>

 <?php 
echo readfile(http://example.com/); // requireAllow_url_includeenable
// OR
echo include(http://example.com/); // requireAllow_url_includeenabled
// OR
echo file_get_contents(http://example.com/);
// OR
echo stream_get_contents(fopen('http://example.com/',rb)); //你可以使用r而不是rb//需要启用Allow_url_fopen
?>

2) Better Way是CURL

  echo get_remote_data('http://example.com/?var2=something&var3=blabla'); // GET request 
echo get_remote_data('http://example.com/','var2 = something& var3 = blabla'); // POST请求


//查看更新和说明:https://github.com/tazotodua/useful-php-scripts/

function get_remote_data ($ url,$ post_paramtrs = false){
$ c = curl_init();
curl_setopt($ c,CURLOPT_URL,$ url);
curl_setopt($ c,CURLOPT_RETURNTRANSFER,1);
if($ post_paramtrs){
curl_setopt($ c,CURLOPT_POST,TRUE);
curl_setopt($ c,CURLOPT_POSTFIELDS,var1 = bla&。$ post_paramtrs);
} curl_setopt($ c,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ c,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ c,CURLOPT_USERAGENT,Mozilla / 5.0(Windows NT 6.1; rv:33.0)Gecko / 20100101 Firefox / 33.0);
curl_setopt($ c,CURLOPT_COOKIE,'CookieName1 = Value;');
curl_setopt($ c,CURLOPT_MAXREDIRS,10);
$ follow_allowed =(ini_get('open_basedir')|| ini_get('safe_mode'))? false:true;
if($ follow_allowed){
curl_setopt($ c,CURLOPT_FOLLOWLOCATION,1);
} curl_setopt($ c,CURLOPT_CONNECTTIMEOUT,9);
curl_setopt($ c,CURLOPT_REFERER,$ url);
curl_setopt($ c,CURLOPT_TIMEOUT,60);
curl_setopt($ c,CURLOPT_AUTOREFERER,true);
curl_setopt($ c,CURLOPT_ENCODING,'gzip,deflate');
$ data = curl_exec($ c);
$ status = curl_getinfo($ c);
curl_close($ c);
preg_match('/(http(| s)):\ / \ /(.*?)\ /(.*\ /|)/si', $ status ['url' $ link);
$ data = preg_replace('/(src | href | action)=(\'| \)(??(http | https | javascript:| \ / \ / | \ / ))。*?)(\'| \)/ si','$ 1 = $ 2'$ link [0]。'$ 3 $ 4 $ 5',$ data);
$ data = preg_replace('/(src | href | action)=(\'| \)(??(http | https | javascript:| \ / \ /)).* ?)(\'| \)/ si','$ 1 = $ 2'$ link [1]。'://'。$ link [3]。'$ 3 $ 4 $ 5',$ data);
if($ status ['http_code'] == 200){
return $ data;
} elseif($ status ['http_code'] == 301 || $ status ['http_code'] == 302){
if(!$ follow_allowed){
if $ redirURL)){
if(!empty($ status ['redirect_url'])){
$ redirURL = $ status ['redirect_url'];
}
} if(empty($ redirURL)){
preg_match('/(Location:| URI:)(。*?)(\r | \\\
)/ si ',$ data,$ m);
if(!empty($ m [2])){
$ redirURL = $ m [2];
}
} if(empty($ redirURL)){
preg_match('/ href\ = \(。*?)\ < \ / a \> / si',$ data,$ m);
if(!empty($ m [1])){
$ redirURL = $ m [1];
}
} if(!empty($ redirURL)){
$ t = debug_backtrace();
return call_user_func($ t [0] [function],trim($ redirURL),$ post_paramtrs);
}
}
}返回ERRORCODE22 with $ url !!< br />最后状态码< b /> ;:。 json_encode($ status)。 < br />< br />最后数据已获得< br />:$ data;
}

注意:它会自动处理FOLLOWLOCATION问题+网址会自动重新更正! (src =./ imageblabla.png--------> src =http://example.com/path/imageblabla.png)



pson GNU / Linux发行版服务器,您可能需要安装 php5-curl 包才能使用它。


I wanna put the content of a URL in a string and the process it. However, I have a problem.

I get this error:

Warning: file_get_contents(http://www.findchips.com/avail?part=74ls244) [function.file-get-contents]: failed to open stream: Redirection limit reached,

I have heard this comes due to page protection and headers, cookies and stuff. How can I override it?

I also have tried alternatives such as fread along with fopen but I guess I just don't know how to do this.

Can anyone help me please?

解决方案

1) local simplest methods

<?php
echo readfile("http://example.com/");   //needs "Allow_url_include" enable
//OR
echo include("http://example.com/");    //needs "Allow_url_include" enabled
//OR
echo file_get_contents("http://example.com/");
//OR
echo stream_get_contents(fopen('http://example.com/', "rb")); //you may use "r" instead of "rb"  //needs "Allow_url_fopen" enabled
?> 

2) Better Way is CURL:

echo get_remote_data('http://example.com/?var2=something&var3=blabla');     // GET request 
echo get_remote_data('http://example.com/', 'var2=something&var3=blabla' ); // POST request


//See Updates and explanation at: https://github.com/tazotodua/useful-php-scripts/

function get_remote_data($url, $post_paramtrs = false) {
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, $url);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    if ($post_paramtrs) {
        curl_setopt($c, CURLOPT_POST, TRUE);
        curl_setopt($c, CURLOPT_POSTFIELDS, "var1=bla&" . $post_paramtrs);
    } curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:33.0) Gecko/20100101 Firefox/33.0");
    curl_setopt($c, CURLOPT_COOKIE, 'CookieName1=Value;');
    curl_setopt($c, CURLOPT_MAXREDIRS, 10);
    $follow_allowed = ( ini_get('open_basedir') || ini_get('safe_mode')) ? false : true;
    if ($follow_allowed) {
        curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
    }curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 9);
    curl_setopt($c, CURLOPT_REFERER, $url);
    curl_setopt($c, CURLOPT_TIMEOUT, 60);
    curl_setopt($c, CURLOPT_AUTOREFERER, true);
    curl_setopt($c, CURLOPT_ENCODING, 'gzip,deflate');
    $data = curl_exec($c);
    $status = curl_getinfo($c);
    curl_close($c);
    preg_match('/(http(|s)):\/\/(.*?)\/(.*\/|)/si', $status['url'], $link);
    $data = preg_replace('/(src|href|action)=(\'|\")((?!(http|https|javascript:|\/\/|\/)).*?)(\'|\")/si', '$1=$2' . $link[0] . '$3$4$5', $data);
    $data = preg_replace('/(src|href|action)=(\'|\")((?!(http|https|javascript:|\/\/)).*?)(\'|\")/si', '$1=$2' . $link[1] . '://' . $link[3] . '$3$4$5', $data);
    if ($status['http_code'] == 200) {
        return $data;
    } elseif ($status['http_code'] == 301 || $status['http_code'] == 302) {
        if (!$follow_allowed) {
            if (empty($redirURL)) {
                if (!empty($status['redirect_url'])) {
                    $redirURL = $status['redirect_url'];
                }
            } if (empty($redirURL)) {
                preg_match('/(Location:|URI:)(.*?)(\r|\n)/si', $data, $m);
                if (!empty($m[2])) {
                    $redirURL = $m[2];
                }
            } if (empty($redirURL)) {
                preg_match('/href\=\"(.*?)\"(.*?)here\<\/a\>/si', $data, $m);
                if (!empty($m[1])) {
                    $redirURL = $m[1];
                }
            } if (!empty($redirURL)) {
                $t = debug_backtrace();
                return call_user_func($t[0]["function"], trim($redirURL), $post_paramtrs);
            }
        }
    } return "ERRORCODE22 with $url!!<br/>Last status codes<b/>:" . json_encode($status) . "<br/><br/>Last data got<br/>:$data";
}

NOTICE: It automatically handes FOLLOWLOCATION problem + Remote urls are automatically re-corrected! ( src="./imageblabla.png" --------> src="http://example.com/path/imageblabla.png" )

p.s.on GNU/Linux distro servers, you might need to install the php5-curl package to use it.

这篇关于获取url内容PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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