cURL不返回任何东西?
问题描述:
<?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的结果,但不是。我做错了什么?cURL不返回任何东西?
答
确实如此,如果你设置了“/”末(固定两个其他错误的拼写)的完整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;
答
的变量中的两个被命名为严重:
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故障到一个空白页(自变量被看作是一个常数,而是常数必须是标量)。
另一个问题可能是用户代理。如果没有设置用户代理,我已经看到完全拒绝回复的服务器。
答
您可以使用一个类,我写了周围卷曲该套。要安装,请参阅: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();
}
希望帮助...
尝试一些调试的东西,例如''var_dump($ content);','curl_setopt($ ch,CURLOPT_HEADER,1);''var_dump(curl_getinfo($ ch));' - 你可能得到一个零长度响应,或者cURL可能遇到一些种类的连接错误。 – Dereleased 2011-04-26 22:17:59
您正在设置正确的页面吗?这只是我过去犯过的一个愚蠢的错误。 – esqew 2011-04-26 22:18:16
始终使用此:error_reporting(E_ALL); ini_set(“display_errors”,1);如果您不希望错误消息弄乱HTML结果,请将它们写入日志文件。 – SteAp 2011-04-26 22:30:31