3

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.

Salman Riyaz
  • 808
  • 2
  • 14
  • 37
  • What do do you mean by convert to HTML? Have you ran `json_deocde` instead of `json_encode`? What is the expected output? – Script47 Jul 05 '18 at 12:53
  • You are making barely any sense here. Please go read [ask] and [mcve], and then edit your question accordingly. – CBroe Jul 05 '18 at 12:56

2 Answers2

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.

Lovepreet Singh
  • 4,792
  • 1
  • 18
  • 36
1

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

apokryfos
  • 38,771
  • 9
  • 70
  • 114