curl problem

Discussion in 'PHP' started by MyVodaFone, Sep 1, 2010.

  1. #1
    Ok I give up, this piece of code works fine on my localhost server but it wont work on my hosts server, however I do have other curl scripts running fine that connect to the same site.

    Its as if the script is not executing at all, the page just loads, where as on my localhost it takes just that bit longer to echo the data.

    Could someone please take a look.
    
    <?php
    
    $id = ""; // DP members enter your facebook profile ID 
    
    // DP members enter your facebook email and password, sorry, remember not to post them back if you find a solution.
    $login_email = 'email@';
    $login_pass = 'password';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php');
    curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&pass='.urlencode($login_pass).'&login=Login');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    
    curl_exec($ch);
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_URL, "http://www.facebook.com/profile.php?id=$id");
    
        $page = curl_exec($ch);
    
    
    // retrieve avatar and person
    preg_match('#<img class="img" src="(http://profile.ak.fbcdn.net/[^<]+)" alt="([^<]+)" />#i',$page, $item);
    		$avatar = $item[1];		
            $person = $item[2];
    
    
    
    echo $person;
    echo "<br />";
    echo $avatar;
        
    curl_close($ch);
    
    
    ?>
    
    PHP:
    With regards to the my_cookies.txt ? I am unsure about that, I don't have to do anything on localhost, nor are any text files created
     
    MyVodaFone, Sep 1, 2010 IP
  2. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Hi MyVodaFone,

    I can suggest you to set
    error_reporting(E_ALL);
    PHP:
    before exec for debug purposes.

    Also you can upload an empty file "my_cookies.txt" and chmod it to 777 (via ftp client) .

    Let me know the result.

    Regards :)
     
    koko5, Sep 1, 2010 IP
  3. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #3
    Just a notice about offset ie: no match for either item, strange that the regex works on my localhost, I'll work backwards and try to match something simpler.

    ... my_cookies.txt doesn't get populated.
     
    MyVodaFone, Sep 1, 2010 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    If I echo $page after $page = curl_exec($ch);

    echo $page;

    It will echo the page on my localhost but not on my hosts server, so maybe the problem is before all that.
     
    MyVodaFone, Sep 1, 2010 IP
  5. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #5
    Did you check phpinfo() on the host and did you try to run other curl scripts there?

    If you try to fetch any other site, which doesn't require to login (as FB) what happens?
    It works fine - seems to be server configuration issue...
     
    koko5, Sep 1, 2010 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    I set up a $text variable on my localhost to point to my_cookies.txt file which caught the cookie from facebook, the text file created was 831bytes

    I found out that unless you use curl_close($ch); no cookie will be created, hence my post above said my_cookies.txt was not been populated, I guess I was fooling around with the script when I said that, but now however I am getting a cookie on my hosts server, but its only 241bytes ?

    The thing is, if I copy that cookie code from my localhost to my hosts server and comment out //curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt"); the script works, because its reading an already formed cookie.

    Something is stopping the script/facebook cookie from been created fully.

    I can post both cookies here, but I am not sure if its safe for me to do that ?

    Besides that would it give any clues, because I guess a cookie doesn't last for ever, so I still need a solution, if anyone has any ideas.
     
    MyVodaFone, Sep 1, 2010 IP
  7. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #7
    I don't know what's going on at all :confused:, today everything is working as it should, but listen thanks for the help and suggestion.

    @KOKO I don't seem to be able to +rep you, I must have in the past, already.
     
    MyVodaFone, Sep 2, 2010 IP
  8. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #8
    Maybe yesterday your server was overloaded so it couldn't set/write entire cookie data...
    Yes, you did on other thread - thanks :) I'm glad if I helped a little bit
     
    koko5, Sep 2, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    Your regex is not escaped properly and is too greedy, change it to the following...

    preg_match('#<img class="img" src="(http://profile\.ak\.fbcdn\.net/[^"]+)" alt="([^"]+)" />#i',$page, $item);
    PHP:
    Edit: check out the following thread for the rest of the expressions -> http://forums.digitalpoint.com/showthread.php?t=1905065#post14773676
     
    Last edited: Sep 2, 2010
    danx10, Sep 2, 2010 IP
  10. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #10
    Actually I'm using a different match now:
    
    preg_match('#<img class=\\\\"logo img\\\\" src=\\\\"(http:\\\\/\\\\/profile.ak.fbcdn.net\\\\/[^<]+)\\\\" alt=\\\\"([^<]+)\\\\" id=\\\\"profile_pic\\\\" \\\\/>#i',$page, $item);
    
    
    PHP:
    But your saying I should be using ([^"]+) instead of this ([^<]+) theres only one match on the page.. but I'll look in to the difference.
     
    MyVodaFone, Sep 2, 2010 IP