Sohan Patel
Sohan Patel

Reputation: 183

How to fetch the url content using php curl or file_get_content on the same server?

I have some code to convert a PHP page to HTML:

$dynamic = "http://website.net/home.php";
$out = "home.html" ;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"$dynamic");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$file = curl_exec($ch);

file_put_contents($out, $file);

This works perfect in localhost but it takes too much time/doesn't work on the live site.

I've tried php_get_contents, but that also doesn't work.

Note:

EDIT:

The web page of my website is loading fast as usual but when I use curl or file_get_contents, it's too slow and even can't get output.

Upvotes: 3

Views: 2252

Answers (5)

Rajesh Ujade
Rajesh Ujade

Reputation: 2735

I think you have DNS resolving problem.

There is two ways you can use your website's locahost instead of the external domain name.

1. If you have your own server / VPS / Dedicated server, Add entry in vim /etc/hosts for 127.0.0.1 website.net or try to fetch the content with localhost(127.0.0.1).

2. If you are using shared hosting, then try to use below url(s) in your code,

http://localhost.mywebsite.net/~username/home.php. 
OR
Try to call http://localhost.mywebsite.net/home.php

Try this to fetch the url content -

$dynamic = TRY_ABOVE_URL(S)
$out = "home.html" ;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$dynamic);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$file = curl_exec($ch);

if($file === false) {
    echo 'Curl error: ' . curl_error($ch);
}

file_put_contents($out, $file);

Upvotes: 2

Stanimir Stoyanov
Stanimir Stoyanov

Reputation: 1213

Try this one:

file_put_contents("home.html", fopen("http://website.net/home.php", 'r'));

but curl should work as well. If you have SSH access to the server, try to resolve your domain name using ping or something. It is possible to have a local DNS issue - it will explain why you can download from external domains.

For my example, you'll need allow_fopen_url to be On, so please verify that by phpinfo() first.

Upvotes: 0

Skarpi Steinthorsson
Skarpi Steinthorsson

Reputation: 521

Looks like it is a network routing issue, basically the server can't find a route to website.net(itself). This has is a server issue and not a PHP issue. The quickest solution is to edit the hosts file on the server and set website.net to 127.0.0.1.

On linux servers you will need to add the following line to the bottom of /etc/hosts to:

127.0.0.1 website.net

Alternatively you can try fetching http://127.0.0.1/home.php but that will not work if you have multiple virtual hosts on the server.

Upvotes: 0

Hendry Tanaka
Hendry Tanaka

Reputation: 464

I think it depends on the provider SSL configuration.

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

Check for know smth. about SSL configuration of your provider.

Upvotes: 0

Apul Gupta
Apul Gupta

Reputation: 3034

To investigate the reason why is it not working on live server, Try this:

$dynamic = "http://website.net/home.php";

$out     = "home.html" ;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $dynamic);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$file = curl_exec($ch);

if($file === false)
{
    echo 'Curl error: ' . curl_error($ch);
}

curl_close($ch); 

file_put_contents($out, $file);

Upvotes: 0

Related Questions