Jump to content

API json redirecting to login


Boxerman
Go to solution Solved by ignace,

Recommended Posts

Evening all!

 

I'm running into an issue where a site i have signed up to provides an API, however when I try to decode it on my server it outputs:

 

 

 

NULL bool(true)

 

The PHP script being used:

$url = file_get_contents("http://apiwebsite.com/blah?latest");
$json = json_decode($url, true); // decode the JSON into an associative array
echo '<pre>' .var_dump($json, true). '</pre>';

When i try to browse to this site on my server it redirects to a website.

 

Is there a way to bypass the login? or actually login to get the API?

 

Thanks everyone

Link to comment
Share on other sites

This may disappoint you, but none of us is a mind reader. We cannot identify the API you're using just by looking at the words “NULL bool(true)”, and we aren't able to deduce the inner workings of that API from this piece of (non-)information. Sure, it would make life a lot easier, but unfortunately, we're mere mortals.

 

Any halfway professional API is extensively documented (if yours isn't, look for an alternative). So the first step is to RTFM. Have you done that? The authentication procedure will involve credentials like an API key which you've received during the registration procedure and which has to be submitted in an API-specific way.

Link to comment
Share on other sites

Hey dude

 

I completely understand, there is no key or anything, the API, I'd rather not sure due to forum rules but for example; the URL is blah.com/user-api/thing?latest

 

If I'm logged in that link works and it displays; however if I'm not logged in and I go to that URL it directs me to the login page.

 

There's no api key at all simply that URL works when logged in but not if you're a 'guest'

 

There is no manual to read but it's still the best API I have found.

 

I'm at a loss on what else to try.

 

Hope this makes sense, I know this reply is like throwing your head against the wall but I don't know what else to answer

Link to comment
Share on other sites

  • Solution

You need to programmatically log in. Something like this:

 

function getSessionCookie($username, $password) {
    $context = [
        'http' => [
            'method' => 'POST',
            'content' => http_build_query(['username_formfield' => $username, 'password_formfield' => $password]),
        ]
    ];
    $contents = file_get_contents('http://apiwebsite.com/login', false, $context);

    if (false === $contents) {
        return false;
    }

    foreach ($http_response_header as $header) {
        if (0 === strpos($header, 'Set-Cookie')) {
            return explode(':', $header)[1];
        }
    }
    
    return false;
}

function queryApi($sessionCookie, $apiCall) {
    $context = [
        'http' => [
            'header' => ["Cookie: $sessionCookie"],
        ]
    ];

    $contents = file_get_contents('http://apiwebsite.com/blah?latest', false, $context);

    // your code
}
You'll still need to handle if the cookie expires etc.. and probably also need to parse the set-cookie header instead of simply copy-pasting. But this should get you started. Edited by ignace
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.