php - 如何在同一台服务器上使用 php curl 或 file_get_content 获取 url 内容?

标签 php caching curl file-get-contents

我有一些代码可以将 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 页面位于托管代码的同一站点中。
  • 根据 phpinfo()
  • curl enabledallow_url_fopen on在本地主机和服务器上。

编辑:

  • 当使用其他网站页面而不是我的网站时,它工作正常。

  • 网站的目标页面在我的浏览器中完美加载。

我网站的网页加载速度和往常一样快,但是当我使用curlfile_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/

相关文章:

php - PHP Built-in Webserver 有内存限制吗?

php - 如何 "upgrade"现实世界中的数据库?

PHP 不显示表中的最后一列

c# - OptimisticConcurrencyException:使用共享的AppFabric缓存和相同的数据库的多个基于EF的应用程序

angularjs - 带有 angularjs 的 Paypal API

php 框架,数据在 var_dump 中有效,但在 echo 中无效

caching - redis持久化是否阻塞读写请求

android picasso 无效不工作

php - 如何通过 Curl 登录到使用 javacookies 的 https?

php - PHP 中的多线程、多 curl 爬虫