【发布时间】:2013-12-27 17:30:57
【问题描述】:
我正在使用 php cURL 来处理登录网站的 POST 请求。有没有办法确定登录失败的时间?我通过使用正确的凭据成功登录进行了实验,$ch 返回 true,但当我使用不正确的凭据时,$ch 也返回 true。我知道当凭据不正确时(通过浏览器),它会将我重定向回主登录页面。
有什么想法吗?
function httpPost($url, $params)
{
try {
//open connection
$ch = curl_init($url);
//set the url, number of POST vars, POST data
// curl_setopt($ch, CURLOPT_COOKIEJAR, "cookieFileName");
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//execute post
$response = curl_exec($ch);
extract($_POST);
echo $response;
//close connection
curl_close($ch);
if (FALSE === $ch)
throw new Exception(curl_error($ch), curl_errno($ch));
// ...process $ch now
}
catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
}
【问题讨论】:
标签: php visual-studio-2012 curl http-post