Jump to content

accessing urls with fopen


ezekielle

Recommended Posts

I'm having trouble accessing urls from a remote server.

For example:

 

<?php

$myFile = "http://www.google.com";

$c = fopen($myFile, "r");

echo $c;

?>

 

This times out. I'm not sure how to configure a wrappers to allow or disallow reading from urls.

I'm brand new to php, and I've spent a couple hours surfing, trying to figure this out.

Any suggestions?

Link to comment
Share on other sites

Not sure if that's what you what you want but anyway...

<?php

$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);
?> 

 

Also look at curl manual http://php.net/curl

Link to comment
Share on other sites

Preferred way

 

if (function_exists('curl_init')) {

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

return curl_exec($ch);

} else {

return file_get_contents($url);

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.