cURL没有返回任何内容?

6
<?php
error_reporting(-1);
$config = array
(
    "siteURL"        => "http://domain.com",
    "loginCheck"     => "checkuser.php",
    "userAgent"      => "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16"
);

$postFields = "username=user&password=pass&submit= Login ";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config['siteURL'] . $config['loginCheck']);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, config['userAgent']);
curl_setopt($ch, CURLOPT_REFERER, config['siteURL']);

$content = curl_exec($ch);
curl_close($ch);

echo $content;

?>

我期望这里应该会输出curl的结果,但实际上并没有。我做错了什么吗?


4
尝试一些调试工具,例如 var_dump($content);curl_setopt($ch, CURLOPT_HEADER, 1);var_dump(curl_getinfo($ch));。你可能会收到一个零长度的响应,或者 cURL 可能会遇到某种连接错误。请注意,这些工具可能需要进行进一步的修改才能调试您的代码。 - Dereleased
你正在使用 cURL 访问的页面设置正确了吗?这是我过去犯过很多次的一个愚蠢错误。 - esqew
始终使用以下代码:error_reporting(E_ALL); ini_set("display_errors", 1); 如果您不希望错误消息混乱您的HTML结果,请将它们写入日志文件。 - SteAp
@Dereleased,它返回bool(false) - @esqew,是的,它已经正确设置了。- @Stefan Pantke,“error_reporting(-1)”相当于E ALL,并且我将其输出到终端。 - Rob
补充Stefan的评论,您可以通过将register_shutdown_function()error_get_last()结合使用来创建一个简单(但非常有用)的“调试器”脚本。 - Christian
实际上,在 PHP 5.3 中 E_ALL 的值是30719。-1 没有相应的错误常量。然而, error_reporting() 函数似乎会将 -1 转换为 E_ALL。主要区别在于可以将 E_PARSE 从 E_ALL 中移除,但无法将 E_PARSE 从 -1 中移除。 :) - Christian
4个回答

8
如果您在URL末尾添加'/',则会生效(修正其他两个拼写错误):
error_reporting(-1);
$config = array
(
    "siteURL"        => "http://www.apple.com/",
    "loginCheck"     => "checkuser.php",
    "userAgent"      => "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16"
);

$postFields = "username=user&password=pass&submit= Login ";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config['siteURL'] . $config['loginCheck']);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $config['userAgent']);
curl_setopt($ch, CURLOPT_REFERER, $config['siteURL']);

$content = curl_exec($ch);
curl_close($ch);

echo $content;

2
我忘了那个斜杠,本来想之后再加上的,但是忘记了。现在可以用了,谢谢。 - Rob

5
如果curl没有返回任何内容,那么您需要检查发生这种情况的原因。请检查最后一个curl错误消息:
if(curl_exec($ch) === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    echo 'Operation completed without any errors, you have the response';
}

基本上,始终检查这一点是很好的。您不应该假设curl请求成功。


我怎样才能知道错误发生的日期?以便确定该错误是来自此请求还是其他请求。 - Agustín Tamayo Quiñones
当您遇到错误(if条件的第一部分)时,您可以获取当前日期/时间并将其打印/保存。您不需要从curl库中获取它。 - MilanG

1

你的两个变量命名不规范:

curl_setopt($ch, CURLOPT_USERAGENT, config['userAgent']);
curl_setopt($ch, CURLOPT_REFERER, config['siteURL']);

应该是:

curl_setopt($ch, CURLOPT_USERAGENT, $config['userAgent']);
curl_setopt($ch, CURLOPT_REFERER, $config['siteURL']);

我猜测 PHP 发生了故障导致页面变成了空白(因为该变量被视为常量,但常量必须是标量)。

另一个问题可能是用户代理。我见过一些服务器如果没有设置用户代理就会完全拒绝响应。


不,原问题与这个字符串“http://www.apple.com”有关,应该是“http://www.apple.com/”。 - SteAp
实际上,在真正的代码执行之前,语法错误就会被发现和报告,所以我报告的问题必须更早地修复。 - Christian
当然。你是对的。但即使有这两个修复,由于URL格式不正确,代码仍然没有输出任何内容。 - SteAp

1

你可以使用我编写的一个封装了CURL的类。要安装,请参见:https://github.com/homer6/altumo

这个类更易于使用,可以为你提供一些易于访问的调试信息。例如:

try{

    //load class autoloader
        require_once( __DIR__ . '/loader.php' );  //you should ensure that is is the correct path for this file

    //make the response; return the response with the headers
        $client = new \Altumo\Http\OutgoingHttpRequest( 'http://www.domain.com/checkuser.php', array(
            'username' => 'user',
            'password' => 'pass',
            'submit' => ' Login '
        ));
        $client->setRequestMethod( \Altumo\Http\OutgoingHttpRequest::HTTP_METHOD_POST );

    //send the request (with optional arguments for debugging)           
        //the first true will return the response headers
        //the second true will turn on curl info so that it can be retrieved later
        $response = $client->send( true, true );

    //output the response and curl info
        \Altumo\Utils\Debug::dump( $response, $client->getCurlInfo() );

    //alternatively, you can get the response wrapped in an object that allows you to retrieve parts of the response
        $http_response =  $client->sendAndGetResponseMessage( true );
        $status_code = $http_response->getStatusCode();
        $message_body = $http_response->getMessageBody();
        $full_http_response = $http_response->getRawHttpResponse();
        \Altumo\Utils\Debug::dump( $status_code, $message_body, $full_http_response );


}catch( \Exception $e ){

    //This will display an error if any exceptions have been thrown
    echo 'Error: ' . $e->getMessage();

}

希望这有所帮助...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接