How to get web page content without CURL?

I need to get the contents of a webpage, I can not use Curl, because it is not included. I tried the code below, but it does not work.

$opts = array( 'http'=>array( 'method'=>"GET", 'header'=>"Accept-language: en\r\n" . "Cookie: foo=bar\r\n" ) ); $context = stream_context_create($opts); $fp = fopen($_GET['url'], 'r', false, $context); if($fp) fpassthru($fp); fclose($fp); exit; 

The code causes an error

 Warning: fopen(http://www.google.com/search?&q=site:www.myspace.com+-intitle:MySpaceTV+%22Todd Terje%22) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request 
+4
source share
6 answers

Did you notice that your URL has the ACTUAL space between Todd and Terrier? This may cause your problem as the browser usually encodes it at + or %20 .

+4
source

you can use old-fashioned code, for example:

 $CRLF = "\r\n"; $hostname = "www.something.com"; $headers[] = "GET ".$_GET['url']." HTTP/1.1"; $headers[] = "Host: ".$hostname; $headers[] = "Accept-language: en"; $headers[] = "Cookie: foo=bar"; $headers[] = ""; $remote = fsockopen($hostname, 80, $errno, $errstr, 5); // a pinch of error handling here fwrite($remote, implode($CRLF, $headers).$CRLF); $response = ''; while ( ! feof($remote)) { // Get 1K from buffer $response .= fread($remote, 1024); } fclose($remote); 

Update: The good thing about this solution is that it does not rely on fopen wrappers.

+4
source

To do this, you can use the file_get_contents function:

 $content = file_get_contents('url/filepath here'); echo $content; 

Note. . If you want to read from a secure protocol, such as https, make sure you have the openssl extension from php.ini.

Update:

From what you are saying, I suspect that you have disabled the allow_url_fopen settings from the php.ini file, you need to enable this in order to read the urls.

Update 2:

It seems you didn’t provide the correct URL, I just checked, for example, if you just entered www.google.com , it works fine:

 $url = 'http://www.google.com'; $content = file_get_contents($url); echo $content; 
+3
source

In fact, you can specify the URL instead of the file name in file_get_contents .

+1
source

use a sniffer like WireShark to get the contents of the actual browser request. Then copy it and delete one at a time, soon you will get the minimum necessary headers.

0
source
  php file_get_contents() function 

nadeausoftware.com/articles/2007/07/php_tip_how_get_web_page_using_fopen_wrappers

  /** * Get a web file (HTML, XHTML, XML, image, etc.) from a URL. Return an * array containing the HTTP server response header fields and content. */ function get_web_page( $url ) { $options = array( CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_HEADER => false, // don't return headers CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_ENCODING => "", // handle all encodings CURLOPT_USERAGENT => "spider", // who am i CURLOPT_AUTOREFERER => true, // set referer on redirect CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect CURLOPT_TIMEOUT => 120, // timeout on response CURLOPT_MAXREDIRS => 10, // stop after 10 redirects ); $ch = curl_init( $url ); curl_setopt_array( $ch, $options ); $content = curl_exec( $ch ); $err = curl_errno( $ch ); $errmsg = curl_error( $ch ); $header = curl_getinfo( $ch ); curl_close( $ch ); $header['errno'] = $err; $header['errmsg'] = $errmsg; $header['content'] = $content; return $header; } 

thanks: http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curl

-1
source

Source: https://habr.com/ru/post/1311456/


All Articles