i have an API that retrieves stock price data through a PHP script. I have tried several different ways to add the price
to my database with no luck.
please help as this is driving me crazy, I have tried logging it as a float with different syntax but still doesn't display,
please could you be kind enough to give me an example using the code below as i am completely new to PHP/mysqli and programing in general.
thanks.
please note that I use import.io to create the API and have removed the API key.
This is how the API works:
<?PHP
$url = 'http://my api url.com';
$json =file_get_contents($url);
$retVal =json_decode($json, TRUE);
$output = "<ul>";
foreach($retVal['results'] as $results) {
$output .="<p>".$results['price/_source']."</p>";
}
$output .="</url>";
echo $output; // this would show the price like so: 1.139791
?> '
//this is the json file.
{
"offset": 0,
"results":
[
{
"price": "1.140 293"
}
`]`,
"cookies": [ ],
"connectorVersionGuid": "2af429a0-3fa4-4e67-b9bf-fbf453674ccb",
"connectorGuid": "87343523-d4c8-47db-b582-5408be1b679b",
"pageUrl": "http://www.x-rates.com/calculator/?from=EUR&to=USD&amount=1",
"outputProperties":
[
`{`
`"name": "price",`
`"type": "STRING"`
`}`
`]`
}
Related
I have a problem with fetching the correct data from a decoded JSON file. I don't know if my question is correct since I don't really know what I am doing for the moment.
So, this is what I don't want to do.
$ln = 'https://api.steamprices.net/v2/csgoprices/?id='.market_hash_name.'&key=XXX';
$link1 = file_get_contents($ln);
$myarray1 = json_decode($link1, true);
echo $myarray1['median_price'];
I am trying to get the price for every steam skin that's being loaded in my code. What this code does is that it loads this api link for every item I load. So if I have 50 items, this link will be loaded 50 times, which is not accepted by the API.
What I want to do, is that I want to load it once, and fetch the prices for every item from that exact link. That link would look like this:
https://api.steamprices.net/v2/csgoprices/?&key=XXX
So, lets say I load it once, and then when I want to apply market_hash_name to it, how do I do?
I assume it is something like this.
$priceJson = file_get_contents('https://api.steamprices.net/v2/csgoprices/?key=XXX');
$priceData = json_decode($priceJson, true);
echo $priceData[''.$market_hash_name.'']['price'];
But it doesn't seem to work. I am sorry for this messy explanation, I an unfamiliar with this.
Note that an example response for the api link looks like this:
{
"-r-H1Z1 Shirt": {
"price": 0.11,
"image": "https://steamcommunity-a.akamaihd.net/economy/image/iGm5OjgdO5r8OoJ7TJjS39tTyGCTzzQwmWl1QPRXu8oaf69-NOHLAbqw_23aLe8AcRQ8-3uyKA7_CGvsJYds9U65FMF7i6AbXTJ8PDm57EliZdK7KLPuuh3dxC3m4m0ihzss0MKE6NtIt4qs-JukOX73WgETXYze_pxEBA",
"game": "h1z1"
},
"2016 Invitational Crate": {
"price": 0.09,
"image": "https://steamcommunity-a.akamaihd.net/economy/image/iGm5OjgdO5r8OoJ7TJjS39tTyGCTzzQwmWl1QPRXu8oaf69-NOHLAbqw_23aLe8AcRQ8-3uyKA7_CGvsJYds9U65FMF7i6APSjJ6BjX9rGBYZ9ioCPzysSX6hNNacA",
"game": "h1z1"
},
"ANGRYPUG Motorcycle Helmet": {
"price": 0.17,
"image": "https://steamcommunity-a.akamaihd.net/economy/image/iGm5OjgdO5r8OoJ7TJjS39tTyGCTzzQwmWl1QPRXu8oaf69-NOHLAbqw_23aLe8AcRQ8-3uyKA7_CGvsJYds9U65FMF7i6AbXTJ8PDm57EliZdK7KLPuuh3WySnxyXoUgz870MKd7sFTkZq98oW1ORiqAVsCUYfbNu3SUQqvUSGyY__iEw",
"game": "h1z1"
},
Another output
{
"name":"Aces High Pin",
"price":1210,
"have":2,
"max":9,
"rate":95,
"tr":0
}
Well, the json string you provide isn't valid but something like this may help you
<?php
$jsonData=file_get_contents("json.file"); // simply contains your json string as posted
$jsonArray=json_decode($jsonData,true);
$jsonObject=json_decode($jsonData);
$list_of_MHN=array("2016 Invitational Crate","ANGRYPUG Motorcycle Helmet");
print_r($jsonArray);
exit;
foreach($jsonArray as $hash_name=>$arr){
if(in_array($hash_name,$list_of_MHN)){
print_r($arr);
}
}
for($i=0;$i<count($list_of_MHN);$i++){
if(isset($jsonArray[$list_of_MHN[$i]])){
print_r($jsonArray[$list_of_MHN[$i]]);
}
}
for($i=0;$i<count($list_of_MHN);$i++){
if(isset($jsonObject->$list_of_MHN[$i])){
print_r($jsonObject->$list_of_MHN[$i]);
}
}
?>
this is the code I am working on. I am creating an API for my mobile device.
<?php
$property_id=$_GET['property_id'];
$res_image=mysql_query("select *
from fak_property_image
where property_id='".$property_id."'");
$i=0;
while($row2=mysql_fetch_array($res_image)){
// $status[$i]['property_id']=$row2['property_id'];
// $status[$i]['image']=$row2['image'];
$status[$i]['path']=$row2['path'];
$i++;
}
echo json_encode( $status );exit;
?>
the output is following
[
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0008.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0009.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0013.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0015.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0016.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0074.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0210.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0211.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1400.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1903.1525441844.jpg"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1904.1525441844.jpg"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1905.1525441844.jpg"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1906.1525441844.jpg"}
]
but my requirement is to display the output like this
[
{
"path":
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0008.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0009.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0013.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0015.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0016.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0074.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0210.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0211.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1400.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1903.1525441844.jpg",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1904.1525441844.jpg",:
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1905.1525441844.jpg",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1906.1525441844.jpg"
}
]
I have tried to fetch all the data using mysql_fetch_all but it is not working. Please help.
Thanks
Try the following:
$status['path'][] = $row2['path'];
Instead of:
$status[$i]['path']=$row2['path'];
But you definitely should use PDO, besides this code is vulnerable for SQL injections.
I am trying to figure out how to echo the genre value from an object created with this programme wrapper that requests json data from 'The Movie Database'. I'm so stuck and grappling to understand Object-oriented PHP so any help would be great.
I think the fact it is 'nested' (if that's the correct terminology) might be the issue.
<?php
include("tmdb/tmdb-api.php");
$apikey = "myapi_key";
$tmdb = new TMDB($apikey, 'en', true);
$idMovie = 206647;
$movie = $tmdb->getMovie($idMovie);
// returns a Movie Object
echo $movie->getTitle().'<br>';
echo $movie->getVoteAverage().'<br>';
echo '<img src="'. $tmdb->getImageURL('w185') . $movie->getPoster() .'"/></li><br>';
echo $movie->genres->id[28]->name;
?>
All of the other values are echoed just fine but I can't seem to get at genres. The json data looks like this ( some of it).
{
"adult":false,
"backdrop_path":"\/fa9qPNpmLtk7yC5KZj9kIxlDJvG.jpg",
"belongs_to_collection":{
"id":645,
"name":"James Bond Collection",
"poster_path":"\/HORpg5CSkmeQlAolx3bKMrKgfi.jpg",
"backdrop_path":"\/6VcVl48kNKvdXOZfJPdarlUGOsk.jpg" },
"budget": 0,
"genres":[
{ "id": 28, "name": "Action" },
{ "id": 12, "name": "Adventure" },
{ "id": 80, "name": "Crime" }
],
"homepage":"http:\/\/www.sonypictures.com\/movies\/spectre\/",
"id":206647,
"imdb_id":"tt2379713",
"original_language":"en",
"original_title":"SPECTRE",
"overview":"A cryptic message from Bond\u2019s past sends him on a trail to uncover a sinister organization. While M battles political forces to keep the secret service alive, Bond peels back the layers of deceit to reveal the terrible truth behind SPECTRE."
}
$movie->genres->id[28]->name
This assumes that id is an array and you want the item with index number 28 from it. What you want is the item containing an id with the value 28 without knowing its index number.
There's no easy way to get to it. You'd have to loop over the array $movie->genres and output the right one.
Maybe like this:
$n = 28;
// loop over genres-array
foreach($movie->genres as $i=>$g){
// check if id of item has right value and if so print it
if($g->id == $n){
echo $g->name;
// skip rest of loop if you only want one
break;
}
}
I'm getting this kind of response from instagram server.
{
"meta": {
"code": 200
},
"data": {
...
},
"pagination": {
"next_url": "...",
"next_max_id": "13872296"
}
}
How do I get the "data" - part? I've tried to json decoding in PHP like:
//$userfeed is giving me something like above.
$tried_this = json_decode($userfeed['meta']);
but $userfeed and $tried_this seems to be the same.
UPDATE
Down below is the REAL data...
I've left out the access token, but otherwise it's correct. This is just a part of it, but I hope you get the picture....
{"pagination":{"next_url":"https://api.instagram.com/v1/users/3/media/recent?access_token=blablabla\u0026max_id=622063574553989866_3","next_max_id":"622063574553989866_3"},"meta":{"code":200},"data":[{"attribution":null,"tags":[],"type":"image","location":{"latitude":21.367158921,"name":"Pali
Lookout","longitude":-157.79304912,"id":60507},"comments":{"count":313,"data":[{"created_time":"1401835727","text":"Can
you give me a shout
out","from":{"username":"nick_putukian1","profile_picture":"http://images.ak.instagram.com/profiles/profile_1370615750_75sq_1401835573.jpg","id":"1370615750","full_name":"Nicholas
Putukian"},"id":"734973811849433422"},{"created_time":"1401836165","text":"I
only have one
follower","from":{"username":"nick_putukian1","profile_picture":"http://images.ak.instagram.com/profiles/profile_1370615750_75sq_1401835573.jpg","id":"1370615750","full_name":"Nicholas
Putukian"},"id":"734977485287985692"},{"created_time":"1401837312","text":"Dear
#kevin could u please add share feature on IG? So users don't have to
screenshoot a foto first if we want to share it.
Thanks.","from":{"username":"natalia.igaa","profile_picture":"http://images.ak.instagram.com/profiles/profile_1003500786_75sq_1401603184.jpg","id":"1003500786","full_name":"Ayu
Natalia"},"id":"734987110351638699"},{"created_time":"1401837882","text":"HI
KEVIN","from":{"username":"gildathegriffon","profile_picture":"http://images.ak.instagram.com/profiles/profile_320785380_75sq_1401742420.jpg","id":"320785380","full_name":"Doivid"},"id":"734991884560110057"},{"created_time":"1401838561","text":"\ud83d\ude02\ud83d\ude02\ud83c\udf42\ud83d\udc9
Forgive me for not giving you a "readable" var_dump, but for some reason the var_dump on a specific server I'm trying on doesn't make it readable as expected.
$data = json_decode($userfeed, true);
var_dump($data['data']);
is returning NULL
Assuming, a valid JSON string, you would do:
$data = json_decode($json_string, true);
var_dump($data['data']);
Lets assume that you have provided access_token, than following syntax will meet your requirements
$url = "https://api.instagram.com/v1/users/3/media/recent/?access_token=ACCESS-TOKEN";
$content = file_get_contents($url);
$data = json_decode($content, true);
var_dump($data['data']);
I'm trying to connect with the instagram API, the connection works fine and I am receiving the updates just as described in the API documentation, the issue is that I cannot access to the data send to my callback function.
According to the doc
When someone posts a new photo and it triggers an update of one of your subscriptions, we make a POST request to the callback URL that you defined in the subscription
This is my code :
// check if we have a security challenge
if (isset ($_GET['hub_challenge']))
echo $_GET['hub_challenge'];
else // This is an update
{
// read the content of $_POST
$myString = file_get_contents('php://input');
$answer = json_decode($myString);
// This is not working starting from here
$id = $answer->{'object_id'};
$api = 'https://api.instagram.com/v1/locations/'.$id.'/media/recent?client_secret='.INSTA_CLI_SECRET.'&client_id='.INSTA_CLI_ID;
$response = get_curl($api); //change request path to pull different photos
$images = array();
if($response){
$decode = json_decode($response);
foreach($decode->{'data'} as $item){
// do something with the data here
}
}
}
Displaying the $myString variable I have this result, don't know why it is not decoded to json :(
[{"changed_aspect": "media", "subscription_id": 2468174, "object":
"geography", "object_id": "1518250", "time": 1350044500}]
the get_curl function is working fine when I hardcode my $id.
I guess something is wrong with my $myString, unfortunately the $_POST cvariable is not populated, Any idea what I am missing ?
Looking at the example JSON response included in your question, I can conclude that the object you are trying to talk with is wrapped in an array (hence the [ and ] around it in the JSON string).
You should access it using $answers[0]->object_id.
If that doesn't work, you can always use var_dump to check out the data in one of your variables.