1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help with Curl needed

Discussion in 'PHP & Perl' started by madblacker, Jun 16, 2010.

Tags:
  1. madblacker

    madblacker Regular Member

    Joined:
    Nov 2, 2009
    Messages:
    268
    Likes Received:
    19
    I've given up on using imacros and am now learning Curl, I already know PHP fairly well at this point, I'm testing this to login to twitter and make a tweet post, I know this can be done via the API but its more that I want to be able to automate any web tasks (so this is helping me learn) and its bothering me that I can't get this to work and also I want to register accounts via curl on twitter which can't be done via the api.

    One thing that I found weird is that I have to post the login POST as the url for it to work.. when I watch the output of the 2nd curl call (where it attempts to post the tweet) it shows the page as having the tweet entered in the input box but it doesn't go through and actually make the tweet.. I've already spent countless hours trying to figure this out.. I got the POST calls from wireshark, using the full TCP package (or whatever its called)... I would greatly appreciate any advice

    <CODE>

    <?php


    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "C:/Users/a/work-current/cookies/my_cookies.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "C:/Users/a/work-current/cookies/my_cookies.txt");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0) ;
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0) ;

    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3)
    //Gecko/20100401 Firefox/3.6.3");

    curl_exec($ch);


    #### set curl login post string
    $test_username = 'myuseraccnt';
    $test_password = 'mypassword';

    $curl_login_post = "https://twitter.com/sessions?authenticity_token=c3884f449f86484198b26fc0c8adc79275ad4552&authenticity_token=c3884f449f86484198b26fc0c8adc79275ad4552&return_to_ssl=false&session[username_or_email]=". $test_username . "&session[password]=". $test_password . "&commit=Sign+In" ;

    // login via curl
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_login_post);
    curl_setopt($ch, CURLOPT_URL, $curl_login_post);

    curl_exec($ch);

    // post tweet via curl

    $test_tweet = "authenticity_token=c3884f449f86484198b26fc0c8adc79275ad4552&status=teeessssssstttttttwwwwwwweeeeetttttttt%0A&twttr=true&return_rendered_status=true&lat=&lon=&place_id=&display_coordinates=false" ;


    curl_setopt($ch, CURLOPT_POST, 'TRUE');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_REFERER, 'http://twitter.com');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $test_tweet);
    curl_setopt($ch, CURLOPT_URL, 'http://twitter.com' );

    curl_exec($ch);



    ?>


    </CODE>
     
    Last edited: Jun 16, 2010
  2. Kaimi

    Kaimi Newbie

    Joined:
    Dec 6, 2009
    Messages:
    35
    Likes Received:
    230
    Home Page:
    Code:
    <?php
    #### set curl login post string
    $test_username = 'myuseraccnt';
    $test_password = 'mypassword';
    
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $jar);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_URL, "http://twitter.com/login");
    
    $login_page = curl_exec($ch);
    
    preg_match('/"authenticity_token" type="hidden" value="(.+?)"/', $login_page, $res);
    $token = $res[1];
    
    //login
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, "https://twitter.com/sessions");
    curl_setopt($ch, CURLOPT_POSTFIELDS, "authenticity_token={$token}&return_to_ssl=false&redirect_after_login&session[username_or_email]={$test_username}&session[password]={$test_password}&commit=Sign+In");
    
    $after_login = curl_exec($ch);
    
    preg_match('/"authenticity_token" type="hidden" value="(.+?)"/', $login_page, $res);
    $token = $res[1];
    
    //post tweet
    curl_setopt($ch, CURLOPT_URL, "http://twitter.com/status/update");
    curl_setopt($ch, CURLOPT_POSTFIELDS, "authenticity_token={$token}&status=test&twttr=true&return_rendered_status=true&lat=&lon=&place_id=&display_coordinates=false");
    
    $after_tweet = curl_exec($ch);
    print $after_tweet;
    
    
    ?>
    
     
    • Thanks Thanks x 1
  3. cyklotrial

    cyklotrial Regular Member

    Joined:
    Oct 13, 2008
    Messages:
    248
    Likes Received:
    82
    Location:
    Wonderland
    1)First you have to get Auth Token
    -go to twitter.com
    -parse source code to find auth token
    2)Use this auth toket to log in and post your tweet
     
  4. madblacker

    madblacker Regular Member

    Joined:
    Nov 2, 2009
    Messages:
    268
    Likes Received:
    19
    Thanks for the responses.. yes I am aware of the need for the authenticity code, I manually parsed it for this but was planning to write a php function to do this once I got the posting functionality to work.. I did it before by taking it from the cookies file that I set with curl... I will try the code posted by kaimi.. thanks