5

I've been struggling with Logging in Wordpress for a while now with CURL (PHP). I think I have tried everything, but it seems that I've missed something. I can't get CURL to save my Cookies when I login. The weird thing, is that even though I can't save cookies to file, everything still works more or less, except Upgrading Worpdress Plugins (that is what I am working on currently). I can login to wordpress, and I can even set an automatic ugprade preg_matching the link and curling on it - Wordpress core upgrades with no problems, but for some reason when it comes to plugins - I just can't do it. I had a LOT guesses last night where the problem lies, but nothing did do. Now when I decided to compare the cookies I get from CURL and in FireFox - it turns out that there are no cookies from CURL.

Okay, So here I set the Options (don't mind the commented stuff, I've been checking everything commenting things out and putting them in...).

$cookieFile = 'cookies.txt'; 
$channel = curl_init();

curl_setopt_array($channel, array (
    CURLOPT_SSL_VERIFYPEER => FALSE,
    CURLOPT_TIMEOUT =>  60,
    CURLOPT_FOLLOWLOCATION =>  1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_COOKIEFILE => $cookieFile,
    CURLOPT_COOKIEJAR => $cookieFile,
    CURLOPT_AUTOREFERER =>  true,
    CURLOPT_POSTFIELDS =>   $loginPostData,
    CURLOPT_POST =>  1,
    #CURLOPT_FRESH_CONNECT => 1
    #CURLOPT_FAILONERROR => 1
));
   # curl_setopt ($channel, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
#
# Shorten curl_exec for our $channel
#
function copen($openUrl, $custom = FALSE) {
global $channel;global $siteUrl;
if($custom) {$siteUrl = "";}
curl_setopt ($channel, CURLOPT_URL, $siteUrl.$openUrl);
$result = curl_exec ($channel) or die(curl_error($channel));
echo "<h3> Opening: $openUrl </h3>";
return $result;
}

So I did a search, and tried to get the cookies, and failed with this:

     preg_match('/^Set-Cookie: (.*?);/mi', $result, $m);
     echo(parse_url($m[1]));

I also tried echoing the $cookieFile with file_get_contents() - empty. I checked the file about 15 times now - it is a 777 writable file. I tried writing to it with file_put_contents() and succeeded, so there is error in the file location and/or permissions. I am obviously missing something, I just can't see it.

Best Regards :)

4 Answers 4

6

For some others check if you are using the absolute path to cookie file!

'C:\wamp\www\cookie.txt'

instead of:

'cookie.txt'

curl_setopt($curl, CURLOPT_COOKIEFILE, 'C:\wamp\www\cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEJAR, 'C:\wamp\www\cookie.txt');
Sign up to request clarification or add additional context in comments.

Comments

2

Expierenced same problem and solved it with calling curl_close() at the end. If it's missing, cookies are not saved.

Comments

1

Okay, So I found the solution. After matching the link to crawl with preg_match() from a HTML content, you need to escape the link first (for a reason unknown to me, yet). We do this with: html_entity_decode($match)

Comments

0

Well i have made a web bot last year and that is the code that i used: i remember very well that the script works 100%. Of Course i have a function that checks if i'm logged in and if im not then login();

function init($username,$password,$mainurl){
    global $curl;
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_COOKIEFILE, "$username");
    curl_setopt($curl, CURLOPT_COOKIEJAR, "$username"); 

    login($curl,$username,$password,$mainurl);
}
function login($curl,$username,$password,$mainurl){
    $logindata="some_login_data";
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_URL, "SITE LOGIN PAGE URL");
    curl_setopt($curl, CURLOPT_POSTFIELDS, $logindata);
    return curl_exec($curl);
}

6 Comments

Were you able to keep the cookie/session live after logging in with this ? Meaning that - Could you crawl a page after logging in, without re-logging ?
yes the cookie is saved in the browser. And you don't need another login (only if you close the browser or the cookies expire)
Okay, then I think I know what is the problem I'm having! - What I am attempting is to make PHP itself CURL The page, without opening it in my browser! Still I don't know a fix for this...
Well try to use my code and tell me if it works or not. And why you don't want to open the PHP from your browser?? how you executing the code then?
The script I'm making is meant to be run with CRON once a week or so, so I would automatically update my Wordpress sites.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.