получить содержание URL-адреса PHP

Я хочу помещать содержимое URL в строку и обрабатывать ее. Однако у меня есть проблема.

Я получаю эту ошибку:

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

Я слышал, что это происходит из-за защиты страниц и заголовков, файлов cookie и т. Д. Как я могу переопределить его?

Я также попробовал альтернативы, такие как fread вместе с fopen, но, я думаю, я просто не знаю, как это сделать.

Кто-нибудь может мне помочь?

1) локальные простейшие методы

 <?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) Лучший способ – 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"; } 

УВЕДОМЛЕНИЕ: он автоматически передает проблему FOLLOWLOCATION + Удаленные URL-адреса автоматически корректируются! (src = "./ imageblabla.png" ——–> src = "http://example.com/path/imageblabla.png&quot😉

pson GNU / Linux, вам может потребоваться установить пакет php5-curl для его использования.

Используйте cURL ,

Проверьте, есть ли у вас это через phpinfo();

И для кода:

 function getHtml($url, $post = null) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); if(!empty($post)) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } $result = curl_exec($ch); curl_close($ch); return $result; } 

Вместо этого попробуйте использовать cURL . cURL реализует куклу cookie, а file_get_contents – нет.