Note that there are some explanatory texts on larger screens.

plurals
  1. POExtracting specific value from JSON string with PHP
    text
    copied!<p>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.</p> <p>On the page that it goes to I have the following code: </p> <pre><code>&lt;?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. } ?&gt; </code></pre> <p>That works fine, and echo $obj['app_data']; prints: {q:"id_src=abc123456789",}</p> <p>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:</p> <pre><code>$appdata = $obj['app_data']; $idcode = json_decode($appdata,true); </code></pre> <p>Followed by echo $idcode['q'], but that just prints a single curly bracket. I tried various variations, removal of ' ' marks, removal of true, etc.</p> <p>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.</p> <p>Thanks for the help all!</p> <p>EDIT: Snippet of $json added. echo $json; prints the following</p> <pre><code>{"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}}} </code></pre> <p>Would it be the comma after the id_src value that is causing problems?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload