<?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;
?>
我希望这能够回应卷曲的结果,但事实并非如此。我做错了吗?
答案 0 :(得分:5)
如果您在结尾处设置了一个带有'/'的完整网址(修复了另外两个拼写错误),则会这样做:
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;
答案 1 :(得分: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出现了空白页面(因为变量被视为常量,但常量必须是标量)。
另一个问题可能是用户代理。如果没有设置用户代理,我已经看到完全拒绝回复的服务器。
答案 2 :(得分: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();
}
希望有帮助...
答案 3 :(得分:1)
我curl不返回任何东西,那么您必须检查发生这种情况的原因。检查最后的卷曲错误消息:
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors, you have the response';
}
基本上,最好始终进行检查。您不应该假定curl请求成功。