简体   繁体   中英

Login using API with PHP and Curl

I'm trying to login and return the profile informations from an API, here are login part of the documentation:

Login using account created specifying email. Requires an existing e-mail based account. A valid login results in a redirection result to the /99/profile page, together with the generation of a new sessionid cookie. The sessionid preserves the user login and has a long duration (lasts for some weeks of inactivity).

And the PHP code they sugest is:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://99motos.apiary.io/accounts/login/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "login=string&password=string");
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
?>

I used this code, changed the "CURLOPT_HEADER" to true and I replaced those strings with my account credentials. Everything worked fine, and it returned:

HTTP/1.1 302 FOUND Date: Wed, 26 Feb 2014 20:04:43 GMT Server: Apache/2.4.6 (Unix) OpenSSL/1.0.1f mod_wsgi/3.4 Python/2.7.6 Vary: Accept-Language,Cookie Content-Length: 0 Location: https://server.99motos.com/99/profile/ Content-Language: pt-br Set-Cookie: sessionid=n8eb7u29n4q7gapfw3b3hlo0q0koi9t0; expires=Wed, 12-Mar-2014 20:04:43 GMT; httponly; Max-Age=1209600; Path=/ Content-Type: text/html; charset=utf-8

There is a "Location" that links to the profile page, but how can I get to those informations on " https://server.99motos.com/99/profile/ "? I know it returns a JSON, but everytime I try to access this link manually, it returns a JSON saying "The user don't exist", I think this is due to the session ID.

In this API, we can get the code to profile, here is the explanation in the documentation,

Profile:

Gives basic user profile information. Despite the unfortunate prefix this is a general accounts page. User must be logged in (with a valid sessionid cookie) to access this page. This page results general "bookkeeping" information about the user, in particular it returns whether the user has completed a registration for becoming an "individual" (user that requests riders) and "rider" (user that accepts order requests) and if the device information, required for receiving PUSH messages is present. The first_login value is deprecated and was replaced by the separate is_individual and is_rider values.

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://99motos.apiary.io/99/profile/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
$response = curl_exec($ch);
curl_close($ch);

var_dump($response);

But likely the previous errors, it returns "The user don't exist", it should return:

200 (OK)
{
    "id": 0,
    "initial_name": "John Doe",
    "first_login": True,
    "is_individual": False,
    "is_rider": False,
    "need_device_info": True,
}

Anyone? ._. Sorry about this looong question... Thanks.

Use follow location option with your curl so that it automatically redirects to the url pointed at your Location:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

Also, use cookie jar to store cookie, and use cookie file to use cookie with request to avoid session related error.

$__FILE_COOKIE = "c:/temp/cookie.txt"; // or /var/tmp/cookie.txt
curl_setopt($ch, CURLOPT_COOKIEJAR, $__FILE_COOKIE);
curl_setopt($ch, CURLOPT_COOKIEFILE, $__FILE_COOKIE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM