开发者

login to any site using curl in php

开发者 https://www.devze.com 2023-02-12 01:26 出处:网络
login to a website using curl p开发者_JAVA百科hp I have tried $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, \"http://www.sitename.com\");

login to a website using curl p开发者_JAVA百科hp

I have tried

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.sitename.com");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_exec($ch);
curl_close($ch);

But this does not log in .


curl_setopt($ch, CURLOPT_POSTFIELDS, true); does not make sense.

You may have CURLOPT_POSTFIELDS and CURLOPT_POST mixed up. The correct way is:

curl_setopt($ch, CURLOPT_POST, true);

Check the manual here: http://no.php.net/manual/en/function.curl-setopt.php


Correct Code should be :

NOTE : change the values for userName an password as required by the login form for the site.

$postField = "userName = abc&password=xyz";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.sitename.com");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_exec($ch);
curl_close($ch);`
0

精彩评论

暂无评论...
验证码 换一张
取 消