Logging into wordpress remotely with cURL

Discussion in 'PHP' started by mbreezy, Jul 12, 2008.

  1. #1
    Trying to log into wordpress with a script and I'm having problems doing so, here's my code. Anyone able to find my mistake?

    
    	$wp_login = 'http://www.example.com/wp-login.php';
    
    	$log = 'admin';
    	$pwd = 'password';
    
    	$ch = curl_init(); 
    	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12");
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    	curl_setopt($ch, CURLOPT_URL, $wp_login); 
    	curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
    	curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
    	curl_setopt($ch, CURLOPT_HEADER , 1); 
    	curl_setopt($ch, CURLOPT_POSTFIELDS, 'log=' . $log . '&pwd=' . $pwd . '&redirect_to=wp-admin/&rememberme=forever&testcookie=1');
    	ob_start(); 
    	$response = curl_exec ($ch); 
    	ob_end_clean(); 
    	curl_close ($ch); 
    	unset($ch); 
    print $response;
    
    Code (markup):
    The only response I'm getting with this is the header info.
     
    mbreezy, Jul 12, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Remove:

    curl_setopt($ch, CURLOPT_HEADER , 1);
     
    Danltn, Jul 12, 2008 IP
  3. mbreezy

    mbreezy Active Member

    Messages:
    135
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #3
    Now it's just not showing up anything... No errors, no headers, looking at the source absolutely no code. :(
     
    mbreezy, Jul 12, 2008 IP
  4. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #4
    Here's what I used to login to WP with:

    
    $log = "admin";
    $password = "password";
    $redirect = "wp-admin/post-new.php";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://www.domain.com/wp-login.php');
    curl_setopt($ch, CURLOPT_POSTFIELDS,'log='.urlencode($log).'&pwd='.urlencode($password).'&redirect_to='.urlencode($redirect));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
    $result = curl_exec($ch);
    
    
    Code (markup):
    Some differences between this code ( I didn't write it, just modified it from a myspace login snippet).
     
    shallowink, Jul 12, 2008 IP
  5. mbreezy

    mbreezy Active Member

    Messages:
    135
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #5
    Nice. Mine was modified from a Yahoo login script. Worked out great, thanks a lot. I just modified it to get the info out of the admin that I needed.

    Thanks tons!
     
    mbreezy, Jul 12, 2008 IP
  6. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #6
    cool. Have you tried to make a post via cURL? I had code to do that but for some reason it would post as a draft. Need to go back and figure out what was wrong with it, just haven't made the time.
     
    shallowink, Jul 12, 2008 IP