繁体   English   中英

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

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

我有一些代码将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);

这在localhost中工作得很好但是它需要太多时间/不能在实时站点上工作。

我试过php_get_contents ,但这也行不通。

注意:

  • http://website.net/home.php页面位于托管代码的同一站点中。
  • curl启用,并且allow_url_fopen按照本地主机和服务器上的 phpinfo() 启用

编辑:

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

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

我网站的网页正常加载,但是当我使用curlfile_get_contents ,它太慢,甚至无法获得输出。

我认为你有DNS解决问题。

有两种方法可以使用您网站的locahost而不是外部域名。

1.如果您有自己的服务器/ VPS / Dedicated服务器 ,请在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

试试这个来获取网址内容 -

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

要调查它无法在实时服务器上运行的原因,请尝试以下操作:

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

我认为这取决于提供商的SSL配置。

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

检查知道smth。 关于您的提供商的SSL配置。

试试这个:

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

但卷曲应该也可以。 如果您具有对服务器的SSH访问权限,请尝试使用ping或其他方式解析您的域名。 可能存在本地DNS问题 - 它将解释您可以从外部域下载的原因。

对于我的例子,你需要让allow_fopen_url为On,所以请先通过phpinfo()验证。

看起来这是一个网络路由问题,基本上服务器找不到到website.net(本身)的路由。 这是一个服务器问题,而不是PHP问题。 最快的解决方案是编辑服务器上的hosts文件并将website.net设置为127.0.0.1。

在Linux服务器上,您需要将以下行添加到/ etc / hosts的底部:

127.0.0.1 website.net

或者,您可以尝试获取http://127.0.0.1/home.php但如果服务器上有多个虚拟主机,则无法使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM