0

while working on facebook connect I have to retrieve an access token from a url ( it is not in the url itself but in the file lined to that url) so this is what I do

$url = "https://graph.facebook.com/oauth/access_token?client_id=".$facebook_app_id."&redirect_uri=http://www.example.com/facebook/oauth/&client_secret=".$facebook_secret."&code=".$code;"

function get_string_between($string, $start, $end){ 
    $string = " ".$string; 
    $ini = strpos($string,$start); 
    if ($ini == 0) return ""; 
    $ini += strlen($start); 
    $len = strpos($string,$end,$ini) - $ini; 
    return substr($string,$ini,$len); 
} 


$access_token = get_string_between(file_get_contents($url), "access_token=", "&expires=");  

it looks ugly and clumsy is there a better way to do it ? thank you .

1 Answer 1

2

You can use the PHP function parse_str

"https://graph.facebook.com/oauth/access_token?client_id=".$facebook_app_id."&redirect_uri=http://www.example.com/facebook/oauth/&client_secret=".$facebook_secret."&code=".$code;

parse_str(file_get_contents($url));

echo $access_token;

From the PHP manual: (link)

void parse_str ( string $str [, array &$arr ] )

Parses str as if it were the query string passed via a URL and sets variables in the current scope.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.