I'm trying to parse the data from the Twitter API in PHP, I would like to receive back the image url media_url_https but I can't get to it. However, I can get the text object fine.
Here's my code:
$data=json_encode($twitter_data);
foreach($data as $item) {
echo $item['text'].'<br/>IMAGE_URL: '.$item['entities']['media']['media_url_https'];
}
Here's the JSON I'm trying to parse:
[
{
"created_at":"Fri Jun 17 22:24:10 +0000 2016",
"id":743932274996056064,
"id_str":"743932274996056064",
"text":"With a heavy heart... https:\/\/t.co\/Dd1M0b7G1P",
"truncated":false,
"entities":{
"hashtags":[
],
"symbols":[
],
"user_mentions":[
],
"urls":[
],
"media":[
{
"id":743932268314497024,
"id_str":"743932268314497024",
"indices":[
22,
45
],
"media_url":"http:\/\/pbs.twimg.com\/media\/ClL6dN3WEAAWykw.jpg",
"media_url_https":"https:\/\/pbs.twimg.com\/media\/ClL6dN3WEAAWykw.jpg",
"url":"https:\/\/t.co\/Dd1M0b7G1P",
"display_url":"pic.twitter.com\/Dd1M0b7G1P",
"expanded_url":"http:\/\/twitter.com\/taylorswift13\/status\/743932274996056064\/photo\/1",
"type":"photo",
"sizes":{
"small":{
"w":680,
"h":519,
"resize":"fit"
},
"thumb":{
"w":150,
"h":150,
"resize":"crop"
},
"medium":{
"w":1200,
"h":915,
"resize":"fit"
},
"large":{
"w":2048,
"h":1562,
"resize":"fit"
}
}
}
]
},
I can't see what's wrong, I'm probably missing something simple! Any help is appreciated!
Related
I'm posting data to my API endpoint. it's simple JSON data. But I'm getting error 404 when posting data as raw JSON but if I post the same data at the same endpoint as raw text its works.
working as raw text
getting error 404 as raw JSON
<?php
var_dump(http_response_code());
echo "hello";
var_dump($_POST);
echo file_get_contents("php://input");
I have removed all code from the API end point and just trying to print post data.
Sample JSON :
{
"object": "whatsapp_business_account",
"entry": [
{
"id": "456",
"changes": [
{
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": "123456789",
"phone_number_id": 123456789
},
"contacts": [
{
"profile": {
"name": "NAME"
},
"wa_id": 123456789
}
],
"messages": [
{
"from": 123456789,
"id": "wamid.ID",
"timestamp": 123456789,
"text": {
"body": "MESSAGE_BODY"
},
"type": "text"
}
]
},
"field": "messages"
}
]
}
]
}
I have checked JSON and it's a valid JSON. no special character but still unable to solve this issue.
One more thing its working on my local machine under XAMPP but not on Linux shared server.
I've got a this json file to import:
{"Spele": {
"Laiks": "2017/01/11",
"Skatitaji": 6740,
"Vieta": "Newlands Stadium",
"T": [
{
"Uzvards": "Antamo",
"Vards": "Dennis"
},
{
"Uzvards": "Prompa",
"Vards": "Pedro"
}
],
"Komanda": [
{
"Nosaukums": "Barcelona",
"Speletaji": {"Speletajs": [
{
"Loma": "V",
"Nr": 16,
"Uzvards": "Sam",
"Vards": "Sidney"
},
{
"Loma": "A",
"Nr": 17,
"Uzvards": "Cisovsky",
"Vards": "Marian"
}
}
]
}
But unfortunately, mongoDBimport imports it as one document. Are there any tips on how to manage trees? I can work with mongo.exe or PHP.
maybe manually adding:
"_id" : { "$oid" : "50906d7fa3c412bb040eb577" }
Would solve it?
i have been working with a similar project some times ago and i parse those as php variables and than store them in database using sql query
first decode json using the follow code:
$response is your json file
$data = json_decode($response);
than you can use this to take data and store to a variable
$newvat = $data->myjson->prices;
I'm using Laravel/PHP to output some JSON for Ember to pick up.... a couple of things here.
First, my PHP looks like this (is there another way to send the data)
return Response::json([
'articles' => $articles->toArray()
], $statusCode);
This is what I am used to doing.
foreach($articles as $article) {
$response['articles'][] = [
'id' => $article->id,
'body' => $article->body,
'title' => $article->title
];
}
return Response::json([
'articles' => $articles->toArray()
], $statusCode);
The first PHP snippet works fine, but the second does not. I get all kinds of errors about resource types by Ember.
Next question is for Ember heads. Right now I am getting everything working with RESTAdapter but should I be using JSONAPIAdapter instead? When I try to get it working with JSONAPIAdapter and JSONAPISerializer I get this error
One or more of the following keys must be present: \"data\",
\"errors\", \"meta
. I can get that error to go away but then I get an error about an undefined type or an unknown resource.
Its not mandatory to use JSONAPIAdapter, but if you have control over the API then you can very well using it. API response should follow the format (http://jsonapi.org/format/)
Sample format for single resource object,
{
"data": {
"type": "articles",
"id": "1",
"attributes": {
// ... this article's attributes
}
}
}
Sample format for multiple resource objects,
{
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
}
}, {
"type": "articles",
"id": "2",
"attributes": {
"title": "Rails is Omakase"
}
}]
}
I am trying to parse JSON data but while json_decode() the var_dump() shows the value as null. Below is my program:
<?php
$json='_variable_1461092903017=[ {
message:"success",
data1:{
datalist:[
{field1:"value1",field2:"value2"} ,
{field1:"value1",field2:"value2"} ,
{field1:"value1",field2:"value2"}
]
},
data2:[ {
Date:"20 Apr 2016",
details:[
{Code:"123",name:"xyz"},
{Code:"456",name:"abc"},
],
},
{
Date:"21 Apr 2016",
details:[
{Code:"123",name:"xyz"},
{Code:"456",name:"abc"},
],
},
{
Date:"22 Apr 2016",
details:[
{Code:"123",name:"xyz"},
{Code:"456",name:"abc"},
],
}
]}
]';
$json_data = json_decode($json);
var_dump($json_data);
?>
Like others said that is not valid JSON
you can debug it using a JSON Linter http://jsonlint.com
A cool valid JSON railroad diagram:
http://www.json.org
This should be what you are looking for (I didn't execute it):
<?php
$json='[{
"message" :"success",
"data1":{
"datalist" :[
{ "field1":"value1","field2":"value2"},
{"field1":"value1","field2":"value2"},
{"field1":"value1","field2":"value2"}
]
},
"data2":[ {
"Date":"20 Apr 2016",
"details":[
{"Code":"123","name":"xyz"},
{"Code":"456","name":"abc"}
]
},
{
"Date":"21 Apr 2016",
"details":[
{"Code":"123","name":"xyz"},
{"Code":"456","name":"abc"}
]
},
{
"Date":"22 Apr 2016",
"details":[
{"Code":"123","name":"xyz"},
{"Code":"456","name":"abc"}
]
}
]}
]';
$json_data = json_decode($json);
var_dump($json_data);
Errors in JSON:
You need to double quote your keys:
{
"key": "value"
}
Not:
{
key: "value"
}
After your closing array bracket ] you are adding a ,
should be this:
{
"datalist": [ "blah", "blah"]
}
Not:
{
"datalist": [ "blah", "blah"],
}
The last element in your array should not have a comma after it:
{
"datalist": [{"key1":"value1", {"key2": "value2"}]
}
Not:
{
"datalist": [{"key1":"value1", {"key2": "value2"},]
}
i have a json file(below) that contains multiple values, it's abit like this -
-makes
- the make
- the model
- year made
- year made
- year made
- the model
- year made
- year made
- the make
- the model
- year made
I can't figure out how to loop through each "make" one by one to get the model and year, Every way iv'e tried so far just seems to produce the make and nothing else.
the output im looking for would look like this(using the snippet below from the full json file)
am general:hummer:1998
am general:hummer:1999
acura:CL:1997
acura:CL:1998
etc..
small snippet of the json file im working with -
{
"makes": [
{
"name": "AM General",
"models": [
{
"name": "Hummer",
"years": [
{
"year": 1998
},
{
"year": 1999
}
]
}
]
},
{
"name": "Acura",
"models": [
{
"name": "CL",
"years": [
{
"year": 1997
},
{
"year": 1998
},
{
"year": 1999
}
]
},
{
"name": "ILX",
"years": [
{
"year": 2013
},
{
"year": 2014
},
{
"year": 2015
}
]
},
{
"name": "ILX Hybrid",
"years": [
{
"year": 2014
}
]
}
]
}
]
}
$data = json_decode($json);
foreach ($data->makes as $make) { // assuming you have gotten this far already...
foreach ($make->models as $model) { // just keep going
foreach ($model->years as $year) { // deeper
echo $make->name . ':' . $model->name . ':' . $year->year . '<br>';
}
}
}
You can open the file using file_get_contents and just decode it :
$file = file_get_contents('path_to_file');
$decoded = json_deconde($file, true); // produces array