1

So I have what appears to be a pretty simple problem, though for the life of me with my lack of coding knowledge I can't figure it out. Excuse any misnomers! So I have a URL heading to a page which contains a signed request with a JSON string in it (it's a Facebook page). I need to retrieve that JSON string and then extract out just a specific part of it. I have no control over the formatting of the URL at the moment.

On the page that it goes to I have the following code:

<?php
$signed_request = $_REQUEST['signed_request']; // Get the POST signed_request variable.

if(isset($signed_request)) // Determine if signed_request is blank.
{
 $pre = explode('.',$signed_request); // Get the part of the signed_request we need.
 $json = base64_decode($pre['1']); // Base64 Decode signed_request making it JSON.
 $obj = json_decode($json,true); // Split the JSON into arrays.

 echo $obj['app_data'];
}
 else
{
die('No signed request avaliable.'); //If there is no signed_request, stop processing script.
}
?>

That works fine, and echo $obj['app_data']; prints: {q:"id_src=abc123456789",}

To me, that in itself is a JSON string, so I thought I could run a json_decode on that and then print out id_src=abc123456789. I tried that like this:

$appdata = $obj['app_data'];
$idcode = json_decode($appdata,true);

Followed by echo $idcode['q'], but that just prints a single curly bracket. I tried various variations, removal of ' ' marks, removal of true, etc.

My end goal is to just extract the abc123456789 value, not the id_src part. I figured it was probably faster to just ask since I'm unlikely to figure it out in the next few days.

Thanks for the help all!

EDIT: Snippet of $json added. echo $json; prints the following

{"algorithm":"HMAC-SHA256","app_data":"{q:\"id_src=abc123456789\",}","issued_at":1333500860,"page":{"id":"380641958713853","liked":false,"admin":true},"user":{"country":"nz","locale":"en_GB","age":{"min":21}}}

Would it be the comma after the id_src value that is causing problems?

1
  • Can you post a snippet of the returned JSON after you base64_decode() it? It's possible your JSON has become corrupted somewhere in the process of fetching it, but we can't know for sure without a snippet of it. Commented Apr 4, 2012 at 0:46

2 Answers 2

2

You can extract what you want like this:

preg_match('/"\\w+?=(\\w+?)"/', $obj['app_data'], $matches);

print_r($matches); // Array ( [0] => "id_src=abc123456789" [1] => abc123456789 )
Sign up to request clarification or add additional context in comments.

2 Comments

My hero! Thanks, print_r($matches[1]) prints exactly what I want.
Thumbs up the answer if it helped you :D
0

{q:"id_src=abc123456789",} is not valid JSON. Try {"q":"id_src=abc123456789"}

1 Comment

Hmmm that's what the json_decode function prints out, and that comes from the decoding of the signed_request passed into the iFrame. So this could mean there is an error in the way app_data is appended to the URL - is there any way to extract the id_src value as is?

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.