Reputation: 23
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?
Upvotes: 1
Views: 1029
Reputation: 51950
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
.
Upvotes: 6