php cURL 不返回任何内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5797112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cURL not returning anything?
提问by Rob
<?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;
?>
I expect this to be echoing the result of the curl, but its not. am I doing anything wrong?
我希望这与卷曲的结果相呼应,但事实并非如此。我做错了什么吗?
回答by SteAp
It does, if you set a complete URL with a '/' at the end (fix two other typos):
确实如此,如果您在末尾设置了一个带有“/”的完整 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;
回答by MilanG
If curl doesn't return anything then you have to check for the reason why that happen. Check for the last curl error message:
如果 curl 没有返回任何内容,那么您必须检查发生这种情况的原因。检查最后的 curl 错误消息:
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors, you have the response';
}
Basically it would be good to always check on that. You shouldn't assume that curl request succeeded.
基本上最好总是检查一下。您不应假设 curl 请求成功。
回答by Christian
Two of your variables are named badly:
您的两个变量命名不当:
curl_setopt($ch, CURLOPT_USERAGENT, config['userAgent']);
curl_setopt($ch, CURLOPT_REFERER, config['siteURL']);
Should be:
应该:
curl_setopt($ch, CURLOPT_USERAGENT, $config['userAgent']);
curl_setopt($ch, CURLOPT_REFERER, $config['siteURL']);
My guess is that PHP faulted to a blank page (since the variable is seen as a constant but constants must be scalar).
我的猜测是 PHP 出错了空白页(因为变量被视为常量但常量必须是标量)。
Another issue may be the user agent. I've seen servers that completely refuse to reply if no user agent was set.
另一个问题可能是用户代理。我见过如果没有设置用户代理,服务器会完全拒绝回复。
回答by Homer6
You can use a class that I wrote that wraps around CURL. To install, see: https://github.com/homer6/altumo
您可以使用我编写的环绕 CURL 的类。要安装,请参阅:https: //github.com/homer6/altumo
It's much easier to use and can give you some easy to access debugging. For example:
它更易于使用,并且可以为您提供一些易于访问的调试。例如:
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();
}
Hope that helps...
希望有帮助...