skip to Main Content

Im hitting this url

https://graph.facebook.com/1843624489016097?fields=link&access_token=EAAD3ZBKkIhYMBAL3KRi9eZBXlYJADZARRFMSe0Nc35WTP92X2etkccVqnjNcjJgKbd8ABtX5pyDPN0nAA7jORyjpOGexZCYp1Sf2iw0DJjCf8UkPiLwhuApSGDGZBvy5w7vk3U0Ba97FZA2DO7J4m4UjvbIolDaRP9TRpemEmLyQZDZD

with the below code in php,

$url ='https://graph.facebook.com/' . $connection->provider_id . '?fields=link&access_token=' . $connection->token;
$ch = curl_init($url);
$json= curl_exec ($ch);

I have this html coming from Facebook, I want to use only “Link” in this,

{"link":"https://www.facebook.com/app_scoped_user_id/YXNpZADpBWEdrUlJiUzc0SWFWZATI4SEVJUmJHTTJQVHU2M3owcTJLOHh5MnJYOTI0LWdMT3VFUC1veXNWdXBhM3o3RzdkQmV4cjNfTC1nSkdheGFhV19pWWU5T1ZAWSzlkN0NBTUl4NVZAKTE9oRjlFbjdObU5i/","id":"1686741234967769"}1

I tried converting that into JSON but its not working, Its coming in same format, since im doing this in a API, im checking this under Postman, I did like this..

$request = json_encode($json, JSON_HEX_QUOT | JSON_HEX_TAG);

The format is not getting convert to json, Im doing in PHP Laravel.

2

Answers


  1. You should use json_decode in place of json_encode. So you should try like:

    $request = json_decode($json, true);
    
    $link = $request["link"];
    

    Also use curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); to save response in variable just after curl_init.

    Login or Signup to reply.
  2. There’s a 1 at the end of the output, possibly you’re echoing something extra that you shouldn’t .

    I suspect you expect curl to return the actual result but you are not using the appropriate flag. The reason I suspect that is because you are assigning the return result to $json but without the flag CURLOPT_RETURNTRANSFERwill return true and not any json value.

    Here’s what you can try:

    $url ='https://graph.facebook.com/' . $connection->provider_id . '?fields=link&access_token=' . $connection->token;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER);
    $json= curl_exec ($ch);
    
    $jsonArray = json_decode($json, true);
    $link = $jsonArray["link"];
    

    More information on the curl flags in the manual

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search