使用curl存储登录的响应? [英] Storing the response of login using curl?

查看:128
本文介绍了使用curl存储登录的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用curl登录到网站使用url,用户名和密码。主要的问题是,我想获得回应,并将其存储在网站已经登录或不是php脚本?

I have logged in to a website using url,username and password using curl.The main question is that i want to get the response back and store it in php script that the site has been logged in or not ?

存储响应,即在php脚本中登录或登录失败是主要问题?

Storing response i.e. logged in or login failed in the php script is the main question ? I have given the script below can u tell me what to add ?

提前感谢

<?php

$cookiefile = tempnam("/tmp", "cookies");

$login_url='**********';


$login_post_url='*********';


$username = "*****";
$password = "*****";

$agent="Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2 ";


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$login_url);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec ($ch);


curl_close ($ch);



$postfields = array(
        'j_username' => $username,
        'j_password' => $password,
    );

$reffer = $login_url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$login_post_url);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
       http_build_query($postfields));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_REFERER, $reffer);


curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);


curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$result = curl_exec ($ch);
 echo $result;

curl_close ($ch);


unlink($cookiefile);
?>


推荐答案


in或php脚本中的登录失败是主要问题?

Storing response i.e. logged in or login failed in the php script is the main question ?

这应该存储在布尔值成功,伴随的HTTP代码。

This should be stored in a Boolean value on success, along with the accompanying HTTP code.

编辑:

登录时可以在第二次cURL调用中使用。 / p>

You can use this in the second cURL invocation when logging in.

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$loginSuccess = $httpCode >= 200 && $httpCode < 400;

登录后也接受重定向。

这篇关于使用curl存储登录的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆