Jump to content

Recommended Posts

I have the following function.

 

It is designed to read a webpage and then show the contents.

 

It works fine on my localhost, but not on the live server, only outputting "Subject: ", $subject isn't shown at all.

 

Any ideas why?

 

<?php
function get_music_player() {
error_reporting(E_ALL);
$url = "www.example.com";

$subject = join('', file($url)); 

return "Subject:  " . $subject;
}
?>

 

The file using the function is as follows.

echo get_music_player();

Link to comment
https://forums.phpfreaks.com/topic/146532-solved-reading-another-webpage/
Share on other sites

Are you on shared hosting? If so then chances are they do now allow fopen_url, they may allow CURL but for file_get_contents to work the server has to have that option set (you can do a phpinfo to find this out).

 

My bet is that is what is going on.

Are you on shared hosting? If so then chances are they do now allow fopen_url, they may allow CURL but for file_get_contents to work the server has to have that option set (you can do a phpinfo to find this out).

 

My bet is that is what is going on.

 

Ah you beat me to it :) I'll have a go at changing the ini.

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

 

curl_exec

Nope. Your host must not like that/had issues with it in the past.

 

imo, switch hosts. There are plenty that at least allow Curl that are decently priced.  You can try using Sockets but I doubt they will work given that the latter two did not.

Yet another question for you.

 

I'm noticing that curl_exec($ch) is outputting the contents of the webpage straight away.

 

How do I assign the contents to a variable so that I can then process it?

 

$contents = curl_exec($ch);

 

The above is just outputting the contents.

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

 

curl_exec and curl_setopt

 

Return Values

 

Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.

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.