PHP Curl 请求不起作用,但在 POSTMAN 中工作正常

2022-01-22 00:00:00 web-scraping postman curl php codeigniter

我正在尝试登录 MCA 门户(POST URL:

知道缺少什么吗?

解决方案

网站做了重定向,所以需要添加

CURLOPT_FOLLOWLOCATION =>1

到您的选项数组.如果对 cURL 有疑问,请尝试

$status = curl_getinfo($curl);echo json_encode($status, JSON_PRETTY_PRINT);

给予:

<代码>{"url": "http://www.mca.gov.in/mcafoportal/loginValidateUser.do?userNamedenc=hGJfsdnk%601t&passwordenc=675894242fa9c66939d9fcf4d5c39d1830f4ddb9&accessCode=-825374456","content_type": "text/plain",http_code":302,header_size":1560,请求大小":245,文件时间":-1,ssl_verify_result":0,重定向计数":0,总时间":1.298891,名称查找时间":0.526375,连接时间":0.999786,pretransfer_time":0.999844,大小上传":0,大小下载":0,速度下载":0,速度上传":0,下载内容长度":0,上传内容长度":-1,starttransfer_time":1.298875,重定向时间":0,"redirect_url": "http://www.mca.gov.in/mcafoportal/login.do","primary_ip": "115.114.108.120",证书信息":[],primary_port":80,"local_ip": "192.168.1.54",本地端口":62524}

如您所见,您获得了 302 重定向状态,但 redirect_count0.添加选项后,我得到:

<代码>{"url": "http://www.mca.gov.in/mcafoportal/login.do","content_type": "text/html;charset=ISO-8859-1",http_code":200,header_size":3131,请求大小":376,文件时间":-1,ssl_verify_result":0,重定向计数":1,总时间":2.383609,名称查找时间":1.7e-5,连接时间":1.7e-5,pretransfer_time":4.4e-5,大小上传":0,大小下载":42380,速度下载":17779,速度上传":0,下载内容长度":42380,上传内容长度":-1,starttransfer_time":0.30734,重定向时间":0.915858,"redirect_url": "","primary_ip": "14.140.191.120",证书信息":[],primary_port":80,"local_ip": "192.168.1.54",本地端口":62642}

EDIT url 编码请求参数,并遵循重定向

 $str = urlencode("userNamedenc=hGJfsdnk%601t&passwordenc=675894242fa9c66939d9fcf4d5c39d1830f4ddb9&accessCode=-825374456");curl_setopt_array($curl , 数组 (CURLOPT_URL =>"http://www.mca.gov.in/mcafoportal/loginValidateUser.do" ,//<- 此处删除参数CURLOPT_RETURNTRANSFER =>真的 ,CURLOPT_ENCODING =>"",CURLOPT_FOLLOWLOCATION =>1、CURLOPT_MAXREDIRS =>10、CURLOPT_TIMEOUT =>30 ,CURLOPT_HTTP_VERSION =>CURL_HTTP_VERSION_1_1 ,CURLOPT_CUSTOMREQUEST =>邮政" ,CURLOPT_POSTFIELDS =>$str,//<- 在此处添加CURLOPT_HTTPHEADER =>大批 (缓存控制:无缓存") ,));

I am trying to login to MCA portal ( POST URL : http://www.mca.gov.in/mcafoportal/loginValidateUser.do )

I tried logging in with POSTMAN app on Google Chrome which works fine. However, it doesnt work either in PHP/Python. I am not able to login through PHP/Python

Here is the PHP Code :

$url="http://www.mca.gov.in/mcafoportal/loginValidateUser.do"; 

$post_fields = array();

$post_fields['userNamedenc']='hGJfsdnk`1t';
$post_fields['passwordenc']='675894242fa9c66939d9fcf4d5c39d1830f4ddb9';
$post_fields['accessCode'] = ""

$str = call_post_mca($url, $post_fields);
$str = str_replace("&nbsp;","",$str);   

$dom = new DOMDocument();
$dom->loadHTML($str);
$xpath = new DOMXPath($dom);

$input_id =  '//input[@id="login_accessCode"]/@value';
$input_val = $xpath->query($input_id)->item(0);
$input_val1 = $input_val->nodeValue;

$url="http://www.mca.gov.in/mcafoportal/loginValidateUser.do"; 

$post_fields['userNamedenc']='hGJfsdnk`1t';
$post_fields['passwordenc']='675894242fa9c66939d9fcf4d5c39d1830f4ddb9';
$post_fields['accessCode'] = $input_val1;  //New Accesscode 

function  call_post_mca($url, $params)
{   
    #$user_agent = getRandomUserAgent();
    $user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36";
    $str = "";
    foreach($params as $key=>$value)
    {
        $str = $str . "$key=$value" . "&";
    }
    $postData = rtrim($str, "&");

    $ch = curl_init();  

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_HEADER, false); 
    #curl_setopt($ch, CURLOPT_CAINFO, DOC_ROOT . '/includes/cacert.pem');

    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);   


    curl_setopt($ch,CURLOPT_USERAGENT, $user_agent);
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt ($ch, CURLOPT_REFERER, $url); 

    $cookie= DOC_ROOT . "/cookie.txt";
    curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
    curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); 

    $output=curl_exec($ch);

    curl_close($ch);
    return $output;

}

Any idea what is missing ?

解决方案

The site does a redirect, so you need to add

CURLOPT_FOLLOWLOCATION => 1

to your options array. When in doubt with cURL, try

$status = curl_getinfo($curl);
echo json_encode($status, JSON_PRETTY_PRINT);

giving :

{
"url": "http://www.mca.gov.in/mcafoportal/loginValidateUser.do?userNamedenc=hGJfsdnk%601t&passwordenc=675894242fa9c66939d9fcf4d5c39d1830f4ddb9&accessCode=-825374456",
"content_type": "text/plain",
"http_code": 302,
"header_size": 1560,
"request_size": 245,
"filetime": -1,
"ssl_verify_result": 0,
"redirect_count": 0,
"total_time": 1.298891,
"namelookup_time": 0.526375,
"connect_time": 0.999786,
"pretransfer_time": 0.999844,
"size_upload": 0,
"size_download": 0,
"speed_download": 0,
"speed_upload": 0,
"download_content_length": 0,
"upload_content_length": -1,
"starttransfer_time": 1.298875,
"redirect_time": 0,
"redirect_url": "http://www.mca.gov.in/mcafoportal/login.do",
"primary_ip": "115.114.108.120",
"certinfo": [],
"primary_port": 80,
"local_ip": "192.168.1.54",
"local_port": 62524
}

As you can see, you got a 302 redirect status, but a redirect_count was 0. After adding the option, i get:

{
"url": "http://www.mca.gov.in/mcafoportal/login.do",
"content_type": "text/html;charset=ISO-8859-1",
"http_code": 200,
"header_size": 3131,
"request_size": 376,
"filetime": -1,
"ssl_verify_result": 0,
"redirect_count": 1,
"total_time": 2.383609,
"namelookup_time": 1.7e-5,
"connect_time": 1.7e-5,
"pretransfer_time": 4.4e-5,
"size_upload": 0,
"size_download": 42380,
"speed_download": 17779,
"speed_upload": 0,
"download_content_length": 42380,
"upload_content_length": -1,
"starttransfer_time": 0.30734,
"redirect_time": 0.915858,
"redirect_url": "",
"primary_ip": "14.140.191.120",
"certinfo": [],
"primary_port": 80,
"local_ip": "192.168.1.54",
"local_port": 62642
}

EDIT url encode the request parameters , and follow redirects

 $str = urlencode("userNamedenc=hGJfsdnk%601t&passwordenc=675894242fa9c66939d9fcf4d5c39d1830f4ddb9&accessCode=-825374456");
curl_setopt_array(
    $curl , array (
    CURLOPT_URL            => "http://www.mca.gov.in/mcafoportal/loginValidateUser.do" , // <- removed parameters here
    CURLOPT_RETURNTRANSFER => true ,
    CURLOPT_ENCODING       => "" ,
    CURLOPT_FOLLOWLOCATION => 1 ,
    CURLOPT_MAXREDIRS      => 10 ,
    CURLOPT_TIMEOUT        => 30 ,
    CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1 ,
    CURLOPT_CUSTOMREQUEST  => "POST" ,
    CURLOPT_POSTFIELDS     => $str,       // <- added this here
    CURLOPT_HTTPHEADER     => array (
        "cache-control: no-cache"
    ) ,
)
);

相关文章