【问题标题】:cURL not getting response from server?cURL 没有得到服务器的响应?
【发布时间】:2014-01-14 17:43:25
【问题描述】:

我正在尝试使用 cURL 在执行 SSH 命令后从其他服务器获取响应。这样做时,我认为这是一个连接错误,因为我没有收到来自服务器的任何响应。所以我制作了一个测试文件,它输出“Works!”,但 cURL 没有从中获取任何东西。它使我的页面进入等待循环,然后退出并且没有响应。

test.php 代码:

<?php echo "works!"; ?>

现在是尝试获取“Works!”的代码。回应。

<?php
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://SERVERIP/test/test.php"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$output = curl_exec($ch); 
curl_close($ch);
echo $output;
?>

另外,如果有帮助,我最近刚刚升级到 CDN 服务器。联系他们,他们说这很可能是软件问题。所以我想我会问你们所有人!

任何关于帮助的回复将不胜感激!

【问题讨论】:

  • 您是否尝试致电curl_error() 和/或curl_getinfo() 来评估响应的情况?
  • 当您从 curl 脚本所在的同一台服务器发出 wget http://SERVERIP/test/test.php 时会看到什么?你有.htaccess rules吗?
  • 测试文件是Test.php还是test.php
  • 它是 test.php,带有小写的 t。让我编辑它并调整标题。但据我所知,我没有任何 .htaccess 规则,因为我在过去两天没有更改它们,并且它在 3 天前工作。我会尝试你给迈克的选项,然后我会回复它。让我尝试做 wget。感谢您的回复!
  • 尝试了 wget 并且成功了。该文件已就位并已下载。至于 curl_error() 和 curl_getinfo():对于 curl_getinfo() 它最初告诉我它需要一个参数,所以我添加了 $ch (请参阅上面的 OP),它给了我这个警告:curl_getinfo() : 10 不是有效的 cURL 句柄资源

标签: php curl dedicated-server


【解决方案1】:

尝试使用CURLOPT_FOLLOWLOCATION

例如:

<?php
//I've added this two lines for debug, run the script and check for errors, when done, comment the lines.  
error_reporting(E_ALL); 
ini_set('display_errors', 1)

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://SERVERIP/test/test.php"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$output = curl_exec($ch); 
curl_close($ch);
echo $output;
?>

根据您的 cmets,我已经更新了答案:

关闭 Linux 服务器上的 PHP 安全模式

Using SSH, connect to your server.
Once you are logged in, type su - root.
For your password, type the same password you used to connect to your server.
At the command prompt type:
vi /etc/php.ini
Go to the line safe_mode = on and press the "i" key.
Change the line to safe_mode = off and press the "Esc" key.
Type :wq! to save your file.

关闭 Windows 服务器上的 PHP 安全模式

Using Remote Desktop Connection, log in to your server as an administrator.  
Open c:\windowsphp.ini in Notepad.  
Change the line safe_mode = on to safe_mode = off.  
Save and close php.ini.  

在 php.ini 上启用 curl:

Windows:

找到 php.ini 并取消注释:

extension=php_curl.dll

Linux:
在带有 apache2 的 debian 上:

apt-get install php5-curl
/etc/init.d/apache2 restart

(php4-curl 如果是 php4)

确保你的 php 编译时支持 curl,使用以下代码编写脚本:

<?php
echo phpinfo();
?>

在那里搜索 CURL。

【讨论】:

  • 不。它仍处于加载循环中。它给了我这个“警告:curl_setopt() [function.curl-setopt]:启用安全模式或设置 open_basedir {URL} 时无法激活 CURLOPT_FOLLOWLOCATION”
  • 让我托管我的网络服务器的公司重新打开它。他们一定是前几天禁用了它。无论如何,感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 1970-01-01
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多