How to remove a specific data from an API? - php

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>';
}

Related

Dependency of HTML rendering on function's return value that executes later?

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>';
}

How to display a JSON API as an Array?

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.

Apple warranty check script

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>";
}
}
?>

PHP sorting results

I have code like this:
<ul>
<?php foreach($persons as $key) : ?>
<?php if($this->session->userdata('id_user') == $key['id_person'] ) : ?>
<li><em><?php echo $key['name'] . " " . $key['surname'] ?></em></li>
<?php else : ?>
<li><?php echo $key['name'] . " " . $key['surname'] ?></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
How can I place result from the if condition at the first place? Can I do here or I need to write SQL query?
EDIT:
This is output:
<ul>
<li>Marko Džunić</li>
<li><em>Saša Miljković</em></li>
<li>mirko mirkovic</li>
</ul>
I want to get this:
<ul>
<li><em>Saša Miljković</em></li>
<li>Marko Džunić</li>
<li>mirko mirkovic</li>
</ul>
<ul>
<?php
$first = '';
$content = '';
$id_user = $this->session->userdata('id_user');
?>
<?php foreach($persons as $key) : ?>
<?php
$id_person = $key['id_person'];
$name = $key['name'];
$surname = $key['surname'];
if ($id_user == $id_person)
{
$first = '<li><em>' . $name . ' ' . $surname . '</em></li>';
}
else
{
$content .= '<li>' . $name . ' ' . $surname . '</li>';
}
?>
<?php endforeach; ?>
<?php echo $first, $content; ?>
</ul>
Go through your array two times.
The first time you only echo when the person is the selected person and then delete this person from the array by using unset().
Go through your array a second time like you did before.
The selected item is now the first item in the list.
Best way is to get the result sorted from the Mysql itself. In MySQL you can place particular rows at the top.

Facebook Graph API : Parsing the output

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>";
}

Categories