I want to know how to POST json object fields based
<?php
$jsondata = file_get_contents('https://newsapi.org/v1/articles?source=google-news&sortBy=top&apiKey=xxxxxxx');
$data = json_decode($jsondata, true);
$tasks = $data['articles'];
foreach ( $tasks as $task) {
$news_title = $task['title'];
$news_url = $task['url'];
$news_imagUrl = $task['urlToImage'];
echo "<option value='".$task."'>".$news_title."</option>"; }
?>
Now, i want to post all the values like title, url, urlToImage on sumbit.
How to achieve this?
Related
I want to get the imdb_id from inside json using foreach
i am having an issue with getting it decoded and displayed.
My php code so far:
$themoviedburl = "http://api.themoviedb.org/3/tv/60625/external_ids?api_key=";
$get = file_get_contents($themoviedburl);
$decode1 = json_decode($get, TRUE);
foreach($decode1['external_ids'] as $value){
$imdb = $value['imdb_id'];
}
Json:
{"id":60625,"imdb_id":"tt2861424","freebase_mid":"/m/0z6p24j","freebase_id":null,"tvdb_id":275274,"tvrage_id":33381,"facebook_id":"RickandMorty","instagram_id":"rickandmorty","twitter_id":"RickandMorty"}
After $decode1 = json_decode($get, TRUE);
You can simply do $imdb_ids = array_column($decode1['external_ids'],'imdb_id'); to collect all imdb_id in one array.
More info on array_column : https://www.php.net/manual/en/function.array-column.php
As discussed in the comments, to get only one imdb_id ID, you can do like below:
$json = '{"id":60625,"imdb_id":"tt2861424","freebase_mid":"/m/0z6p24j","freebase_id":null,"tvdb_id":275274,"tvrage_id":33381,"facebook_id":"RickandMorty","instagram_id":"rickandmorty","twitter_id":"RickandMorty"}';
$decoded_data = json_decode($json,true);
$imdb_id = $decoded_data['imdb_id'];
I need to show data in Attribute and Value form but data is getting is in direct form.
I am trying this
Getting Data in this FOrm
{"FirstName":"sbncf","EmailAddress":"jscn#jnsc.cs","LastName":"jj","Phone":"653736","SearchBy":"jhjnjn"}
But i need in this Form
$data_string = '[{"Attribute":"FirstName","Value":"Bret"},{"Attribute":"LastName","Value":"Lee"},{"Attribute":"EmailAddress","Value":"bret.lee#australia.com"},{"Attribute":"Phone","Value":"8888888888"},{"Attribute":"SearchBy","Value":"Phone"}]';
I am using AJAx and using this
$data_string = json_decode(json_encode($_POST));
enter image description here
You need to store data into an another array, new array must contains Attribute and Value indexes, something like this example:
// testing $_POST data
$post = array();
$post['FirstName'] = 'sbncf';
$post['EmailAddress'] = 'jscn#jnsc.cs';
$post['LastName'] = 'jj';
$newArray = array(); // initialize new array
$i = 0;
foreach ($post as $key => $value) {
$newArray[$i]['Attribute'] = $key;
$newArray[$i]['Value'] = $value;
$i++;
}
//use json_encode here:
$data_string = json_encode($newArray);
echo $data_string;
Result:
[{"Attribute":"FirstName","Value":"sbncf"},{"Attribute":"EmailAddress","Value":"jscn#jnsc.cs"},{"Attribute":"LastName","Value":"jj"}]
How can I get the source, title, issn, author, ... from a json file: JSON file
We tried with:
$new_pmid = $_POST['new_pmid'];
$api_json_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id=".$new_pmid."&retmode=json";
$json = file_get_contents($api_json_url);
$data = json_decode($json, TRUE);
echo $header[0]->result->$new_pmid->title;
....
But nothing happen...
Can you give me the solution for the json file (generated from pubmed database).
Thank you.
You didn't use the $data variable, which stored the decoded data
You decode JSON into $data as array
$title = $data['result'][$new_pmid]['title'];
$issn = $data['result'][$new_pmid]['issn'];
$authors = $data['result'][$new_pmid]['authors'];
--Update--
To get $authors name ,authtype ,... use foreach loop:
foreach($authors as $author){
$name = $author['name'];
$authtype = $author['authtype'];
$clusterid = $author['clusterid'];
}
I'm populating select box with options from json file and trying to set value from $_POST as selected. I get all the values printed as options but none selected.
Seems like something goes wrong when comparing $marke to $post so nothing get selected.
<select name="marke" id="marke"class="form-control">
<?php
$url = 'includes/lists/models.json';
$jsonData = file_get_contents($url);
$jsonDataObject = json_decode($jsonData, true);
$post = $_POST['marke'];
$i = 0;
echo '<option>--</option>';
foreach ($jsonDataObject['markes'] as $marke) {
if ($marke==$post) {
echo '<option selected value="'.$marke['title'].'" id="'.$i.'">'.$marke['title'].'</option>';
}else{
echo '<option value="'.$marke['title'].'" id="'.$i.'">'.$marke['title'].'</option>';
}
$i++;
}
?>
</select>
P.S $_POST['marke'] is set correcly.
As I understand it $marke is an array, and most likely $_POST['marke'] is a string or int.
if ($marke['title']==$post) {
instead of
// You'd be comparing a $marke array with $_POST['marke'] string
if ($marke==$post) {
I'm loading event listing as an JSON-File from an URL:
$file = file_get_contents('http://ecample.com/listing.php?');
$data = json_decode($file);
?>
The direct output looks like this (more values and mor lines):
[{"id":"1","name":"NAME_1","booking_url":"https://ecample.com/Event_ID65654","category_id":"195"},
{"id":"2","name":"NAME_2","booking_url":"https://ecample.com/Event_ID65654","category_id":"195"},
"id":"1","name":"NAME_1","booking_url":"https://ecample.com/Event_ID65654","category_id":"195"}]
I need to search for all entrys with the value "name":"NAME_1" and print out the value of "booking_url".
I tried different things like array_seach() etc. but did not work out.
Any help is appreciated!
try this
<?php
$file = file_get_contents('http://ecample.com/listing.php?');
$data = json_decode($file, true);
foreach ($data as $r)
{
if ($r['name'] == 'NAME_1')
{
echo $r['booking_url'];
}
}
?>
The other option is to access your data in object context, thus:
<?php
$file = file_get_contents('http://ecample.com/listing.php?');
$data = json_decode($file);
foreach ($data as $r)
{
if ($r->name == 'NAME_1')
{
echo $r->booking_url;
}
}
?>
Either one should work just as well.