首页 > 解决方案 > curl 在我的共享主机上无法正常工作

问题描述

我的问题是curl在我的主机上没有以正确的方式工作,但在我的本地主机上运行良好。

这是我用来测试的代码:

$url = 'https://www.instagram.com/nasa/?__a=1';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); 
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result= curl_exec ($ch);
curl_close ($ch);
echo style_print_r( $result );

在我的本地主机中,代码的结果是这样的: 在此处输入图像描述

但是在我的托管中,结果是这样的: 在此处输入图像描述

在我的托管中,它似乎尝试加载整个页面:(

如何解决这个问题?

编辑:这是在我的主机中打印的源代码: 在此处输入图像描述

标签: phpcurlhttpsfile-get-contentsshared-hosting

解决方案


请检查您共享主机中的 Curl

如果您想检查您使用的托管服务是否已激活 CURL,请创建一个包含以下内容的 php 文件:

<?php
   // Script to test if the CURL extension is installed on this server

   // Define function to test
   function _is_curl_installed() {
       if  (in_array  ('curl', get_loaded_extensions())) {
   Â Â Â Â Â Â Â  return true;
   Â Â Â  }
   Â Â Â  else {
   Â Â Â Â Â Â Â  return false;
   Â Â Â  }
   }

   // Ouput text to user based on test
   if (_is_curl_installed()) {
      Â  echo "cURL is <span style=\"color:blue\">installed</span> on this server";
   } else {
      Â  echo "cURL is NOT <span style=\"color:red\">installed</span> on this server";
   }
?>

您可以使用任何文件名保存文件,例如 curl.php,保存后,请通过 Web 浏览器访问它,例如在地址www.yourdomain.com/curl.php以查找结果。


推荐阅读