somejkuser
somejkuser

Reputation: 9040

Curl issue retrieving passed back GET parameters

I am working with an OAuth API. Originally, I was making a header redirect once I received my $token than aggregating all of theGET parameters that were passed back to me. I'm trying to implement curl instead so I don't have to redirect back and forth. My problem is, I don't know how to retrieve the returned GET parameters after making my curl request. Here's my code

        $qry_str = "?oauth_token=" . $token;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, GSAPI_AUTHORIZE_ENDPOINT . $qry_str);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, '3');
        $content = trim(curl_exec($ch));

I've realized I don't need $content. What I need is the GET parameters which should be passed back when I imagine occurs when initiating curl_exec. How do I retrieve these?

Upvotes: 0

Views: 177

Answers (1)

Garry Welding
Garry Welding

Reputation: 3609

What you need to do is get the curl request to return the headers as part of the return string. You then need to parse them for the "Location:" tag and use some of the built in parse functions within PHP to get the data you want. Try the below.

$qry_str = "?oauth_token=" . $token;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, GSAPI_AUTHORIZE_ENDPOINT . $qry_str);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
$content = trim(curl_exec($ch));

if(!$content) echo 'Curl error: ' . curl_error($ch);

preg_match('/Location\:\s(.*)\s/',$content,$matches);
$urlParts = parse_url(trim($matches[1]));
parse_str($urlParts['query'],$queryArray);

The $queryArray variable should now contain all of the query string parameters from the URL in the "Location:" field of the header.

EDIT:

This will work if you hitting a script at http://www.someurl.com/oauth.php and it's then redriecting to http://www.someotherurl.com/somescript.php?param1=x&param2=y.

The result of $queryArray will be array("param1" => 1, "param2" => 2), although I may have completely mis-understood your question...

Upvotes: 2

Related Questions