tags:

views:

213

answers:

6

I need to get webpage's content ,I cant use Curl as it is not enabled.I tried the below code But it is not working.

$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 produce 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 
A: 

You can use the file_get_contents function for that:

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

Note: If you want to read from secure protocol eg https, make sure that you have openssl extension turned on from php.ini.

Update:

From what you say, i suspect you have allow_url_fopen settings turned off from php.ini file, you need to turn that on to be able to read from urls.

Update 2:

It looks you are not specifying the correct url, I just checked, for example, if you simply put in www.google.com, it works fine:

$url = 'http://www.google.com';
$content = file_get_contents($url);
echo $content;
Sarfraz
Yep i already tried it , ERROR " Warning: file_get_contents(<url>) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request "
Arsheep
@Arsheep: See my updated answer please.
Sarfraz
Okay i saw , not a secure url "and allow_url_fopen = On" i checked
Arsheep
@Arsheep: See my updated 2 please.
Sarfraz
+1  A: 

You can actually specify a URL instead of a filename in file_get_contents.

dreeves
i know bro i already tried it , ERROR " Warning: file_get_contents(<url>) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request "
Arsheep
A: 

use sniffer like WireShark to get contents of actual browser request. Then copy it and remove one by one, soon you will get minimal needed headers.

Andrey
+1  A: 

you can use old-fashioned code, like:

$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: Good thing about this solution is that it doesn't rely on fopen wrappers.

mr.b
+2  A: 

Have you noticed that there is an ACTUAL space in your url between Todd and Terje? That might cause your problem, as browser usually encode it to + or %20.

CharlesLeaf
What i can Say "You rules" :P Eagle eye ;) .Yes that problem was !!
Arsheep
To mark this answer as "You rules", click on the green tick mark to the left.
Douglas
A: 
 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;
}

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

WOW , I think you forget the question "How to Get a Webpage’s contents without CURL ?"
Arsheep
I downvoted because the OP didn't want to use CURL. However, if you remove the curl bit and include some example code not using CURL I can probably upvote you.
Nathan Adams