我有一些代码可以将 PHP 页面转换为 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);
这在本地主机上运行完美,但它需要太多时间/在实时站点上不起作用。
我试过 php_get_contents
,但还是不行。
注意:
http://website.net/home.php
页面位于托管代码的同一站点中。
根据 curl
enabled 和allow_url_fopen
on在本地主机和服务器上。
phpinfo()
编辑:
当使用其他网站页面而不是我的网站时,它工作正常。
网站的目标页面在我的浏览器中完美加载。
我网站的网页加载速度和往常一样快,但是当我使用curl
或file_get_contents
时,速度太慢甚至无法输出。
最佳答案
我认为您遇到了 DNS 解析问题。
有两种方法可以使用您网站的本地主机而不是外部域名。
<强>1。如果您有自己的服务器/VPS/专用服务器,
在 vim/etc/hosts
中为 127.0.0.1 website.net 添加条目或尝试使用 localhost(127.0.0.1) 获取内容。
<强>2。如果您使用的是共享主机,请尝试在您的代码中使用以下url(s)
,
http://localhost.mywebsite.net/~username/home.php.
OR
Try to call http://localhost.mywebsite.net/home.php
试试这个来获取 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);
关于php - 如何在同一台服务器上使用 php curl 或 file_get_content 获取 url 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25931168/