Wasn't sure yet whether to post this publicly. I've adjusted the PHP code snippet for the OAuth. Curl allows you to trap a redirect and this snippet just shows how to do that for the token. The regex searches for the redirect url in the returned header, it can be adjusted to make sure it starts with "Location: ".

<?php
    $client_id = '';
    $client_secret = '';

    $current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

    if (!isset($_REQUEST['code']))
    {
        header("Location: http://www.daniweb.com/api/oauth?client_id=$client_id&redirect_uri=" . urlencode($current_url));
    }

    $ch = curl_init('http://www.daniweb.com/api/access_token');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_HEADER, true);

    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
        'code' => $_REQUEST['code'],
        'redirect_uri' => $current_url,
        'client_id' => $client_id,
        'client_secret' => $client_secret
    ));

    $result = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($http_code == 301 or $http_code == 302) {
        preg_match("@https?://([-\w\.]+)+(:\d+)?(/([\w/_\-\.]*(\?\S+)?)?)?@", $result, $m);
        $target_url = $m[0];
        $url_parts = parse_url($target_url);
        parse_str($url_parts['query'], $query_parts);
        $token = $query_parts['access_token'];
        echo 'Your Access Token for this session is ' . $token;
    }
?>

Basically, you can do the same check for the redirect to the oauth page, and only actually redirect if you do not get a code back, because at that point there is no approval yet.

diafol commented: nice +14

Wasn't sure yet whether to post this publicly.

Why not? Feel free to share. Thanks for the improvement!!

Honestly, I don't have much experience with cURL, so providing alternate ways of doing things for people can only help. Thanks for the tip!

Okay, point taken. When I have time I'll do a test on my last statement in my post. If it works I'll update the code, and make it a code snippet (with some comments).

commented: this code deserves another rep! +14

Sounds great :)

On a related note, how has your experience beeen working with the API up until now?

how has your experience beeen working with the API up until now?

Very brief, hope to spend some time with it this weekend, as they are predicting bad weather. When I do, I'll keep you posted.

Sounds good. Looking forward to any improvements and suggestions you may have as you work with it.

Since PHP and C# clients are already done or in the making, I want to make a client in Delphi and/or Lazarus.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.