【问题标题】:cURL not returning anything?cURL 不返回任何东西?
【发布时间】:2011-04-26 22:15:29
【问题描述】:
<?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 的结果相呼应,但事实并非如此。我做错什么了吗?

【问题讨论】:

  • 尝试一些调试的东西,例如var_dump($content);curl_setopt($ch, CURLOPT_HEADER, 1);var_dump(curl_getinfo($ch)); - 您可能会收到零长度响应,或者 cURL 可能会遇到某种连接错误。
  • cURLing 的页面设置正确吗?只是我过去犯过很多次的愚蠢错误。
  • 总是使用这个:error_reporting(E_ALL); ini_set("display_errors", 1);如果您不希望错误消息弄乱您的 HTML 结果,请将它们写入日志文件。
  • @Dereleased,它返回 bool(false) - @esqew,是的,它设置正确。 - @Stefan Pantke,error_reporting(-1) 相当于 E ALL,我将其输出到终端。
  • 添加到 Stefan 的评论中,您可以通过将 register_shutdown_function()error_get_last() 结合来创建一个简单(但非常有用)的“调试器”脚本。

标签: php curl


【解决方案1】:

如果你设置了一个完整的 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】:

如果 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 请求成功。

【讨论】:

    【解决方案3】:

    您的两个变量命名错误:

    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 出错到空白页(因为变量被视为常量,但常量必须是标量)。

    另一个问题可能是用户代理。如果没有设置用户代理,我见过服务器完全拒绝回复。

    【讨论】:

    • 不,原来的问题与这个字符串“apple.com”有关,应该是“apple.com”。
    • 其实是在真正的代码执行之前发现并报告了语法错误,所以我报告的问题必须早点修复
    • 当然。你说的对。但即使有这两个修复程序,代码也会注意到 - 由于 URL 格式错误。
    【解决方案4】:

    您可以使用我编写的包含 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();
    
    }
    

    希望对您有所帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多