I'm just trying to display an API response as an Array, but something is wrong and I don't find it:
The API's answer:
{"status":"OK","minecrafts":[{"id":411,"ip":"2452453","name":"EdenCraft","port":23,"ram":512,"leaf_id":1522,"ftp_password":"1231235312","subscription_end":"2014-02-06T19:56:29.000Z"}]}
My PHP code:
<?php
$url = "http://api.edenservers.fr/minecraft?user_id=id&api_key=key";
$data = file_get_contents($url); // Opening the Query URL
$data = json_decode($data, true); // Decoding the obtained JSON data
if(count($data) > 0) {
foreach($data as $rank => $donnees) {
echo "<u>" . $rank ."</u> : ". $rank;
echo "<br />";
}
}
else {
echo "Rien n'a été troué.";
}
?>
And this is what's showed on my Dashboard:
status : status
minecrafts : minecrafts
Thanks for your help !
Try
foreach($data as $key=>$val){
if(is_array($val)){
foreach($val[0] as $key1=>$val1){
echo "<u>" . $key1 ."</u> : ". $val1;
}
}else{
echo "<u>" . $key ."</u> : ". $val;
}
}
Change:
echo "<u>" . $rank ."</u> : ". $rank
to:
echo "<u>" . $rank ."</u> : ". print_r( $donnees, TRUE )
Or the other way around if you want the values reversed. But the issue is with using $rank in both places.
Related
I'm working on a site which gives metrics of site, it uses API of gtmetrix. So i want to remove a specific data from the coming result but I just dont know how to do it. Some help would be appreciated!
<?php
require_once('service.inc.php');
$test = new Services_WTF_Test("email", "api_key");
$url_to_test = "https://google.me/";
echo "Testing $url_to_test\n";
$testid = $test->test(array(
'url' => $url_to_test
));
if ($testid) {
echo "Test started with $testid\n";
}
else {
die("Test failed: " . $test->error() . "\n");
}
echo "Waiting for test to finish\n";
$test->get_results();
if ($test->error()) {
die($test->error());
}
$testid = $test->get_test_id();
echo "Test completed succesfully with ID $testid\n'<br>";
$results = $test->results();
if ($results): ?>
<?php
foreach($results as $result => $data): ?>
<strong><?php $ukey = strtoupper($result);
echo $ukey; ?>:</strong>
<?php echo $data; ?><br><br>
<?php endforeach;
endif;
?>
The Output Is:
FIRST_CONTENTFUL_PAINT_TIME: 1495
PAGE_ELEMENTS: 44
REPORT_URL: https://gtmetrix.com/reports/google.me/BFylJNX3
REDIRECT_DURATION: 0
FIRST_PAINT_TIME: 1495
DOM_CONTENT_LOADED_DURATION:
DOM_CONTENT_LOADED_TIME: 1908
I want to remove the 3rd data from the api REPORT_URL:
You can skip REPORT_URL in foreach
$ukey = strtoupper( $result );
if( $ukey != 'REPORT_URL' ) {
echo '<strong>' . $ukey . '</strong>';
echo $data . '<br><br>';
}
I have a function that prints "Our data" first and then inside a loop checks for all the data in the database and then prints the data.
However, if there is no data, it shouldn't print "Our data", how do I achieve this since "Our data" is already printed?
The layout is as follows :
<h1> Our Data </h1>
<p> <!-- DATA --> </p>
The function is as follows:
function cpt_info($cpt_slug, $field_name, $title) {
echo "<h1>Our" . $title . "</h1>"; //here I want to print our data
$args = array('limit' => -1);
$cpt = pods($cpt_slug, $args);
$page_slug = pods_v('last', 'url');
echo "<h1 class='projects-under-" . $cpt_slug . "'>" . $title . "</h1>";
if ($cpt->total() > 0) :
while ($cpt->fetch()) :
if (!strcasecmp($cpt->field('post_name'), $page_slug)) :
$data = $cpt->field($field_name);
foreach ((array) $data as $key => $value) {
if ($value != null) : //here I check whether data is empty
$url = wp_get_attachment_url(get_post_thumbnail_id($value['ID']));
echo "<div class='col-sm-3 col-xs-6'><div class='border-to-circle'>";
echo "<div class='circle-container' style='background: url(" . $url . ")center center, url(" . get_template_directory_uri() . '/images/rwp-banner.jpg' . ")center center;'><h4><a href='" . get_permalink($value['ID']) . "'>" . $value['post_title'] . "</a></h4></div>";
echo "</div></div>";
endif;
}
endif;
endwhile;
endif;
}
Something like below:
if(count($data)){
echo '<h1> Our Data </h1>';
}
I have this code but it doesn't work. It show nothing.
<?php
$item = file_get_contents('https://www.googleapis.com/books/v1/volumes?q=Bowling Alone&maxResults=1');
$booktitle = (isset($item->volumeInfo->title) ? $item->volumeInfo->title : false);
$description = (isset($item->volumeInfo->description) ? $item->volumeInfo->description : false);
echo "<b>Title:</b> " . $booktitle;
echo "<b>Description:</b> " . $description;
?>
So, please help me fix it ! Also, this is any way to get content faster ? Thank you very much !
Make it url compatible, so no spaces in it. You also needed to add a foreach loop because incase of more results. And decode the json result into a object
$item = file_get_contents('https://www.googleapis.com/books/v1/volumes?q=Bowling%20Alone&maxResults=1');
$item = json_decode($item);
foreach ($item->items as $item) {
$booktitle = (isset($item->volumesInfo->title) ? $item->volumeInfo->title : false);
$description = (isset($item->volumeInfo->description) ? $item->volumeInfo->description : false);
echo "<b>Title:</b> " . $booktitle;
echo "<b>Description:</b> " . $description;
}
<?php
$item = file_get_contents('https://www.googleapis.com/books/v1/volumes?q=Bowling%20Alone&maxResults=1');
$item = json_decode($item);
$item = reset($item->items);
$booktitle = (isset($item->volumesInfo->title) ? $item->volumeInfo->title : false);
$description = (isset($item->volumeInfo->description) ? $item->volumeInfo->description : false);
echo "<b>Title:</b> " . $booktitle;
echo "<b>Description:</b> " . $description;
?>
After looking for some Ruby scripts i tried to write on PHP with some help the script.
My Problem is now that I am not sure if the jSon Objects are correct cause I dont know the source for it now.
My Question is if I am making anything wrong with jSon in PHP? If not than the Objects of the Sources are wrong.
<?php
$sn = isset($_GET['sn']) ? $_GET['sn'] : '';
if($sn)
{
$url = 'https://selfsolve.apple.com/warrantyChecker.do?sn='.$sn . "&country=USA";
$json = file_get_contents($url);
$json = substr($json, 5, -1);
$json_obj = json_decode($json);
if(isset($json_obj->ERROR_CODE))
{
echo $json_obj->ERROR_DESC;
}
else
{
echo "$json_obj->PROD_DESCR <img src=\"$json_obj->PROD_IMAGE_URL\" alt=\"\"><br>";
echo"Product Description: $json_obj->PROD_DESCR <br>";
echo"Purchase date: $json_obj->PURCHASE_DATE <br>";
echo"Warranty exp date: $json_obj->COVERAGE_DATE <br>";
}
}
?>
<form action="" method="get" accept-charset="utf-8">
<p><input name="sn" value="<?=$sn?>"><input type="submit" value="Lookup serial"></p>
</form>
Another way I have tried to do it is
<?php
$sn = $argv[1];
$data = json_decode(file_get_contents(
"https://selfsolve.apple.com/warrantyChecker.do?sn=". $sn . "&country=USA"));
echo "Product Description" .$data->PROD_DESCR."\n";
echo "Coverage for " . $sn . " ends on " . $data->COVERAGE_DATE . "\n";
?>
<?php
$sn = isset($_GET['sn']) ? $_GET['sn'] : '';
if($sn)
{
$url = 'https://selfsolve.apple.com/warrantyChecker.do?sn='.$sn . "&country=USA";
$json = file_get_contents($url);
//This line you are splitting the json form then it wont work
$json = substr($json, 5, -1);
$json_obj = json_decode($json);
if(isset($json_obj->ERROR_CODE))
{
echo $json_obj->ERROR_DESC;
}
else
{
echo "$json_obj->PROD_DESCR <img src=\"$json_obj->PROD_IMAGE_URL\" alt=\"\"><br>";
echo"Product Description: $json_obj->PROD_DESCR <br>";
echo"Purchase date: $json_obj->PURCHASE_DATE <br>";
echo"Warranty exp date: $json_obj->COVERAGE_DATE <br>";
}
}
?>
I am trying to get the names of my friends using FB Graph API with this call :
$friends = file_get_contents('https://graph.facebook.com/me/friendsaccess_token='.$session["access_token"]);
echo "Friends : $friends\n";
This gives me a list of the form :
{"data":[{"name":"ABC XYZ","id":"12212839"},{"name":"PQR GHI","id":"5004678"}]}
I want to be able to store only the NAMES in an array. How do I use $friends to get the names ? Something like $friends['name'] doesn't seem to work.
Please help.
Thank you.
$friends = json_decode($friends);
foreach($friends['data'] as $friend)
{
echo $friend['name'];
}
The return is a json object, you need to decode it. Although I strongly urge you to use an SDK such as http://github.com/facebook/php-sdk/
If this doesn't work try:
$friends = json_decode($friends);
foreach($friends->data as $friend)
{
echo $friend->name;
}
Here is what I did to get the posts information.. crude but it works. Note that the shares likes comments and reactions are one level deeper in the JSON object
$posts = json_decode($output); // from FB Graph v2.8 API call
foreach($posts->data as $post)
{
echo "MESSAGE: ", $post->message, "<br>";
echo "NAME: ", $post->name, "<br>";
echo "TYPE: ", $post->type, "<br>";
echo "ID: ", $post->id, "<br>";
echo "LINK: ", $post->link, "<br>";
echo "PERMALINK: ", $post->permalink_url, "<br>";
echo "CREATED: ", $post->created_time, "<br>";
if($post->shares->count == "") { $shares = "0"; } else { $shares = $post->shares->count; }
echo "SHARES: ", $shares, "<br>";
if($post->reactions->summary->total_count == "") { $reactions = "0"; } else { $reactions = $post->reactions->summary->total_count; }
echo "REACTIONS: ", $reactions, "<br>";
if($post->comments->summary->total_count == "") { $comments = "0"; } else { $comments = $post->comments->summary->total_count; }
echo "COMMENTS: ", $comments, "<br>";
if($post->likes->summary->total_count == "") { $likes = "0"; } else { $likes = $post->likes->summary->total_count; }
echo "LIKES: ", $likes, "<br>";
echo "<br><br>";
}