Facebook JSON output - json

I have this code
$graph_url = "https://graph.facebook.com/100000070848126/statuses?access_token=".$params['access_token'];
$status = json_decode(file_get_contents($graph_url),true);
echo $status->data->message;
and I'm having a problem on how to output data in the array $status. I just don't know how to call the items for this feed

The second parameter of json_decode() is whether to create an associative array or not.
json_decode ( string $json [, bool $assoc = false])
You specified that you do want an array, so the way you would access the values would be like this -
echo $status['data']['message'];
If you leave out the true parameter of the json_decode() function, you'll be able to access the values in a more object oriented way like the syntax in your question.

Related

store string into array

In MySQL, I have a field name postcodes and the type is LONGTEXT. It stores several postcodes being separated by comma. How would I retrieve that and store it as an array for other use ?
you can use the PHP method explode().
one think you can't do, is to do a where x = x on it in the database.
In the model, you can set the mutator methods:
public function getPostcodesAttribute($value) {
return explode(',',$value);
}
public function setPostcodesAttribute($value) {
$this->attributes['postcodes'] = implode(',',$value);
}
Lets say that you have the result stored in a string like this:
$s = "6000,5447"; //$s = $array->postcodes;
you can get the each value on an index in an array using this:
$values= explode(",", $s);
echo $values[0]; // 6000
Or even better.. you can store it as json, and retrieve it as json in array format.
Store it as a JSON field in MySQL, Laravel encode and decode them when you retrieve and save them respectively
in your migration
$table->json('field_name');
then in the model
protected $json = ['field_name'];
then whenever you access the field, laravel will convert it to an array for you, you don't have to call any explicit methods.
Doc - https://laravel.com/docs/5.7/eloquent-mutators#attribute-casting
// the final array all the post codes are collected.
$postCodes = [];
foreach (Model::pluck('postcodes') as $stringCodes)
foreach (explode(',', $stringCodes) as $postCode) $postCodes[] = $postCode;

How to convert params to array

I receive the following URL
/articles?difficulty=1,2
Inside the framework I want to receive an array from that - [1,2]. Is there any method in the framework to convert params like that into an array automatically? Can the framework do that? I can do like that explode(',', $params['difficulty']) - but I'm wondering whether this can be handled by the framework.
I don't want to pass params like that:
/articles?difficulty[]=1&difficulty[]=2
There is no helper in framework Request component for converting such values, it can be easily achieved with native PHP explode function. Use:
$array = explode(',', $string);
as you suggested.
But the wrapper of explode exists - \yii\helpers\StringHelper::explode(), it has additional options for trimming and skipping empty elements, you can use it too. But most of the times using regular explode should be enough.
try this
Use the Request class.
http://www.yiiframework.com/doc-2.0/yii-web-request.html
print_r(Yii::$app->request->get()); returns all get variables in an array. It's like doing print_r($_GET); in straight php.
If you want a specific $_GET variable you access it as follows:
Yii::$app->request->get('difficulty');
In your case it would be:
$success = Yii::$app->request->get('success');
$token = Yii::$app->request->get('token');
Then after that explode it with comma, so it will get converted into array.
$array = explode(',', $success );

Kohana 2.3.4 : MySQL query to JSON

I am managing a project in Kohana 2.3.4 where I need to create an API for my android backend. What I am doing is send a query on my model which returns $result.
$query = "select product.deal_id,product.deal_key,p..."
$result = $this->db->query($query);
I am not sure if $result is an Object or and array This consists of 4 rows and 8 columns. I need to change $result to json format. I am currently doing this by echoing.
echo json_encode($result);
This return an empty json {}.
I am able to use the same query on my view by iterating through the $result
foreach ($result as $h){
echo $h->main_key;
}
Am I doing this right or is it that my $result on this connection has no rows?
I figured out I had use Kohana debug to know if my result was an object or an array. After calling the following
echo Kohana::debug($result);
I figured out that it was an object hence the empty result when converted to a json object. I had also tried getting an associative array with mysql_fetch_assoc which actually expected a mysql query object. This did
not work as the object was created by my ORM object. I then solved this by calling
$result = $this->db->query($query)->as_array();
This returned an array and solved my problem.

CakePHP find on a json encoded field

I am creating a store in CakePHP and have added a text field which stores a json array of all the categories the store falls into.
How do I do a cake find on all stores that fall into the "gardening" category?
json encoded field:
["gardening","books-and-toys"]
I am thinking some sort of in_array() find but not quite sure.
Many Thanks
You can use the PHP method json_decode and return the JSON object as an associative array by setting the second option of that function to true. You can then call array_search for example to return the corresponding key.
<?php
$categories = json_decode($json, true);
$categoryKey = array_search('gardening', $categories);
$categoryName = $categories[$categoryKey];
?>
You can then use $categoryName in a CakePHP find() call.

Problems parsing Reddit's JSON

I'm working on a perl script that parses reddit's JSON using the JSON module.
However I do have the problem of being very new to both perl and json.
I managed to parse the front page and subreddits successfully, but the comments have a different structure and I can't figure out how to access the data I need.
Here's the code that successfully finds the "data" hash for the front page and subreddits:
foreach my $children(#{$json_text->{"data"}->{"children"}}) #For values of children.
{
my $data = $children->{"data"}; #accessing each data hash.
my %phsh = (); #my hash to collect and print.
$phsh{author} = $data->{"author"};#Here I get the "author" value from "data"
*Etc....
This successfully gets what I need from http://www.reddit.com/.json
But when I go to the json of a comment, this one for example, it has a different format and I can't figure out how to parse it. If I try the same thing as before my parser crashes, saying it is not a HASH reference.
So my question is: How do access the "children" in the second JSON? I need to get both the data for the Post and the data for the comments. Can anybody help?
Thanks in advance!
(I know it may be obvious, but I'm running on very little sleep XD)
You need to either look at the JSON data or dump the decoded data to see what form it takes. The comment data, for example is an array at the top level.
Here is some code that prints the body field of all top-level comments. Note that a comment may have an array of replies in its replies field, and each reply may also have replies in turn.
Depending on what you want to do you may need to check whether a reference is to an array or a hash by checking the value returned by the ref operator.
use strict;
use warnings;
binmode STDOUT, ':utf8';
use JSON;
use LWP;
use Data::Dump;
my $ua = LWP::UserAgent->new;
my $resp = $ua->get('http://www.reddit.com/r/funny/comments/wx3n5/caption_win.json');
die $resp->status_line unless $resp->is_success;
my $json = $resp->decoded_content;
my $data = decode_json($json);
die "Error: $data->{error}" if ref $data eq 'HASH' and exists $data->{error};
dd $data->[1]{data}{children}[0];
print "\n\n";
my $children = $data->[1]{data}{children};
print scalar #$children, " comments:\n\n";
for my $child (#$children) {
print $child->{data}{body}, "\n";
}