3

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:

  • http://website.net/home.php page is in the same site where the code is hosted.
  • curl is enabled and allow_url_fopen is on as per phpinfo() in both localhost and at server.

EDIT:

  • It works fine when using other website's page instead of my website.

  • The site's target page perfectly loads in my browser.

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.

14
  • check your server phpinfo() curl support = enabled and allow_url_fopen=on Commented Sep 19, 2014 at 9:59
  • sure allow_url_fopen = on on the host / server? Commented Sep 19, 2014 at 10:01
  • curl is enabled and allow_url_fopen is on Commented Sep 19, 2014 at 10:06
  • also close your curl after exec Commented Sep 19, 2014 at 10:09
  • 1
    @rajeshujade The side loads as smooth as butter in my machine. Also the ping is good. Commented Sep 25, 2014 at 11:33

5 Answers 5

2
+50

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);
Sign up to request clarification or add additional context in comments.

1 Comment

PHP error Warning: file_put_contents( .... html content ... at line file_put_contents($result['content'], $out);
0

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);

3 Comments

are you able to see any error by curl_error function? Add <?php ini_set('display_errors', 1);error_reporting(E_ALL); ?> in the beginning of script.
Even curl function returns neight true nor false
did you added ini_set('display_errors', 1);error_reporting(E_ALL); in the beginning of script? Also, I updated the code, please use updated one.
0

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.

1 Comment

I don't thinks it will depend on provider SSL configuration
0

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.

9 Comments

The php script is hosted at http://website.net itself.. it is not using external website
why can't server can't access itself
But at shared server editing hosts file is not possible
It's trying to access the external IP address and can't find a way to it, it's similar to not finding the front door of your own house while looking out the window. By editing /etc/hosts you are telling the server that website.net is on the server itself and not outside it. If this is on a shared server then the service provider must fix the routing.
Have you tried with external IP address? If that works then this is a routing issue, not PHP issue.
|
0

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.

4 Comments

Page loading and loading and after several minutes it says Connection timed out in file.php at line xx.
And on line xx you have the file_put_contents... ? I think that your troubles are caused by an DNS issue. Could you do echo gethostbyname("yourdomainname.com"); and match it with the actual IP which your website translates into?
It returns the ip address of server, and yest it matches.
Please re-attempt with curl and file_put_contents, but add curl_setopt($ch, CURLOPT_VERBOSE, true); just before curl_exec($ch);, and also print_r(curl_getinfo($ch)); right after curl_exec($ch); and post the result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.