1

I have a PHP script using cURL that I want to use to retrieve a json file from a remote server. To get to the json file/REST system you need to use authentatication. Basically, I can get it to login and store cookies. But when I try to grab the json page and echo it, it returns "Session is not found". Here is my code:

function login($url,$data) {
    $fp = fopen("cookie.txt", "w");
    fclose($fp);
    $login = curl_init();
    curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($login, CURLOPT_TIMEOUT, 10000);
    curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($login, CURLOPT_URL, $url);
    curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($login, CURLOPT_POST, TRUE);
    curl_setopt($login, CURLOPT_POSTFIELDS, $data);
   // ob_start();
    return curl_exec ($login);
   // ob_end_clean();
    curl_close ($login);
    //unset($login);
}

function grab_page($site){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_TIMEOUT, 40);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch, CURLOPT_URL, $site);
    //ob_start();
    echo curl_exec ($ch);
    //ob_end_clean();
    curl_close ($ch);
}

// Login and retrieve the my meteor page

login('https://www.mymeteor.ie','username=username-removed9&userpass=pass-removed');
echo grab_page('https://www.mymeteor.ie/cfusion/meteor/Meteor_REST/service/prepayBalance');

Can anyone help me with this?

1
  • You should use "CURLOPT_COOKIEFILE" instead of "CURLOPT_COOKIEJAR", when you are trying to send cookies to the server.
    – benck
    Commented Sep 20, 2011 at 20:30

1 Answer 1

6

The CURLOPT_COOKIEJAR is the place where the cookie is placed after a request — stored there for later use.

When the time comes for sending a cookie along with a request, then use CURLOPT_COOKIEFILE to specify which cookie to send.

Your grab_page() function will need to use this latter option, rather than (or as well as, if you need to store any cookies in the response for later) CURLOPT_COOKIEJAR.

3
  • Now I'm getting Session value is null. Any ideas? My cookie.txt is full with the necessary cookies.
    – Shane
    Commented Sep 20, 2011 at 20:32
  • I'm going to guess that the Session value is null. Does your cookie contain that value, or is it something you need to send somewhere else, or is it completely unrelated to what you send?…
    – salathe
    Commented Sep 20, 2011 at 20:34
  • It's completely unrelated to what I send as far as I know.
    – Shane
    Commented Sep 20, 2011 at 20:36

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.