How to get url 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 fine on localhost, but takes too long / doesn't work on a real site.
I tried php_get_contents
it but that also doesn't work.
Note:
-
http://website.net/home.php
the page is on the same site that hosts the code. -
curl
enabled andallow_url_fopen
on according tophpinfo()
both local and server.
EDIT:
-
It works great when using a page from another site instead of my website.
-
The site's landing page loads fine in my browser.
My site's webpage loads fast as usual, but when I use curl
or file_get_contents
, it is too slow and can't even get output.
source to share
I think you have a DNS resolution problem.
There are two ways to use your locahost site instead of an external domain name.
1. If you have your own server / VPS / Dedicated Server , Add an entry vim /etc/hosts
for 127.0.0.1 website.net or try uploading content using localhost (127.0.0.1).
2. If you are using shared hosting try using 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 get the url -
$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);
source to share
To find out the reason why it doesn't work on a real server, try the following:
$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);
source to share
Try the following:
file_put_contents("home.html", fopen("http://website.net/home.php", 'r'));
but curl should work. If you have SSH access to the server, try resolving your domain name using ping or whatever. There may be a local DNS issue - this will explain why you can download from external domains.
In my example, you will need allow_fopen_url
to enable, so check << 22> first.
source to share
It looks like it's a network routing issue, basically the server can't find a route to the website (by itself). This is a server issue, not a PHP issue. The fastest solution is to edit the hosts file on the server and set the website to 127.0.0.1.
On Linux servers, you will need to add the following line at the bottom of / etc / hosts:
127.0.0.1 website.net
Alternatively, you can try fetching http://127.0.0.1/home.php
, but it won't work if there are multiple virtual hosts installed on the server.
source to share