output XML from Yahoo BOSS API with PHP - php

I managed to get this code so far:
<?php
//Gather data and prepare query
$thequery = urlencode($_GET['s']);
$yhost = 'http://boss.yahooapis.com';
$apikey = 'xxxxxxxxxxxxxxx';
$url = $yhost.'/ysearch/news/v1/'.$thequery.'?appid='.$apikey.'&format=xml';
//Get the results
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$results = new SimpleXmlElement($data, LIBXML_NOCDATA);
//echo the results
foreach ($results->resultset_news->result as $theresult) {
echo ''.$theresult->title.'<br/>';
echo $theresult->abstract.'<br/>';
echo '<small><i>'.$theresult->dispurl.'</i></small><br/>';
echo '<br/><br/>';
}
So how exactly do i output actual XML rather than HTML?

Tried this:
echo $results->asXML();
?

Related

PHP - Fortnite API show me "Invalid authentication credentials"?

I would like to make a small Fortnite API, but I always get an error in the JSON file.
{"message":"Invalid authentication credentials"}
My PHP Code:
$ch = curl_init();
//pc, xbl, psn
curl_setopt($ch, CURLOPT_URL, "https://api.fortnitetracker.com/v1/profile/pc/MyName");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'TRN-Api-Key: My-API-Code'
));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
$fp = fopen("myStats.json", "w");
fwrite($fp, $response);
fclose($fp);
$data = json_decode(file_get_contents("myStats.json"));
$solo = $data->stats->p2;//solos data
$duos = $data->stats->p10;//duos data
$squads = $data->stats->p9;//squads data
$matches = $data->recentMatches;//match data
$sesh1 = $matches[0]->id->valueInt;
$solo_wins = $solo->top1->valueInt;
$duos_wins = $duos->top1->valueInt;
$squads_wins = $squads->top1->valueInt;
$solo_matches = $solo->matches->valueInt;
$duos_matches = $duos->matches->valueInt;
$squads_matches = $squads->matches->valueInt;
$solo_kd = $solo->kd->valueDec;
$duos_kd = $duos->kd->valueDec;
$squads_kd = $squads->kd->valueDec;
$solo_games = $solo->matches->valueInt;
$duos_games = $duos->matches->valueInt;
$squads_games = $squads->matches->valueInt;
$solo_kills = $solo->kills->valueInt;
$duos_kills = $duos->kills->valueInt;
$squads_kills = $squads->kills->valueInt;
$total_matches = ($solo_matches+$duos_matches+$squads_matches);
$total_wins = ($solo_wins+$duos_wins+$squads_wins);
$total_kills = ($solo_kills+$duos_kills+$squads_kills);
$total_kd = (round($total_kills/($total_matches-$total_wins),2));
echo 'Total Matches: '.$total_matches.'<br>';
echo 'Total Wins: '.$total_wins.'<br>';
echo 'Total Kills: '.$total_kills.'<br>';
echo 'Total KD: '.$total_kd.'<br>';
echo $sesh1;
?>
I entered the correct API code. Why is this message written in a JSON file and not my wins? It's so crazy because it should work.
What returns on line 11? You are using var_dump($response);
Could you write die(); under that line and provide us the return that it gives?
There might be something wrong on how you handle the headers. I can't check for sure right now as I am not home. But I might be able to test this out later.

How to parse a remote XML file with PHP?

I want to parse the following XML file.
What I have so far is:
$xml = new SimpleXMLElement('http://smarkets.s3.amazonaws.com/oddsfeed.xml', LIBXML_NOCDATA, true);
foreach ($xml->odds->event as $item) {
echo (string)$item->market;
}
But this does not work. Can you help me?
You can try with php CURL:
$ch = curl_init();
$url = 'http://smarkets.s3.amazonaws.com/oddsfeed.xml';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
$data = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($data);
print_r($xml);
I have no idea which information you want to extract so here is an example how to get the attributes 'id' and 'slug' from all market nodes.
Just add compress.zlib:// to your url to get the xml, for PHP 4.3.0 and up
<?php
$xml = simplexml_load_file('compress.zlib://http://smarkets.s3.amazonaws.com/oddsfeed.xml') or die("Error: Cannot create object");
foreach ($xml->event as $item) {
echo $item->market['id'] . "<br>" . $item->market['slug'] . "<br><br>";
}
?>

Get more than 10 results by google search API in php

I am trying to get 10 pages result listed using the following cod below. When i run the URL directly i get a json string but using this in code it does not returns anything. Please tell me where i am doing wrong.
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=CompTIA A+ Complete Study Guide Authorized Courseware site:.edu&start=20";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body,true);
print_r($json);
Now i am using the following code but it outputs only four entries of a page. Please tell me where i am doing wrong.
$term = "CompTIA A+ Training Kit Microsoft Press Training Kit";
for($i=0;$i<=90;$i+=10)
{
$term = $val.' site:.edu';
$query = urlencode($term);
$url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' . $query . '&start='.$i;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body,true);
//print_r($json);
foreach($json['responseData']['results'] as $data)
{
echo '<tr><td>'.$i.'</td><td>'.$url.'</td><td>'.$k.'</td><td>'.$val.'</td><td>'.$data['visibleUrl'].'</td><td>'.$data['unescapedUrl'].'</td><td>'.$data['content'].'</td></tr>';
}
}
Just try with urlencode
$query = urlencode('CompTIA A+ Complete Study Guide Authorized Courseware site:.edu');
$url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' . $query . '&start=20';

Latest Post Facebook page integration to website

<?php
function fetchUrl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$feedData = curl_exec($ch);
curl_close($ch);
return $feedData;
}
$profile_id = "xxxxxxxxxxxxx";
//App Info, needed for Auth
$app_id = "xxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxx";
$authToken = fetchUrl("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret}");
$json_object = fetchUrl("https://graph.facebook.com/{$profile_id}/posts?{$authToken}");
$feedarray = json_decode($json_object);
echo $json_object;
foreach ( $feedarray->data as $feed_data )
{
echo "<h2>{$feed_data->name}</h2><br />";
echo "{$feed_data->message}<br /><br />";
}
?>
When I try to run my code with my details (fully open page), I get an output of
Warning: Invalid argument supplied for foreach() in /home/xxxxxxxxx/public_html/includes/footer.php on line 52
When I use the code below it prints bool(false).
$feedData = curl_exec($ch);
var_dump($feedData); die();
I have worked out that the fetchUrl's are not working and I have tried many different others but none seem to work...
Any ideas?

Parsing XML with SimpleXML returns nothing

I'm currently trying to parse the MapQuest Traffic API, but when I try to display an incident, nothing appears, and if I do "if empty" in php, it returns empty.
Here's the code:
<?php
$mysongs = simplexml_load_file("http://www.mapquestapi.com/traffic/v1/incidents?key=Fmjtd%7Cluuan1u2nh%2C2a%3Do5-96rw5u&callback=handleIncidentsResponse&boundingBox=$_GET[a], $_GET[b], $_GET[c], $_GET[d]&filters=construction,incidents&inFormat=kvp&outFormat=xml");
echo $mysongs->Incidents[0]->Incident[0]->fullDesc;
?>
The parameters I'm passing: ?a=33.352532499999995&b=-118.2324383&c=34.352532499999995&d=-117.2324383.
Thanks in advance!
Here simplexml_load_file not loading all your xml data so, i created a xml file with name test.xml and then loaded data from test.xml. Now you can print data what you need.
<?php
$a = $_GET['a'];
$b = $_GET['b'];
$c = $_GET['c'];
$d = $_GET['d'];
$xml_feed_url = 'http://www.mapquestapi.com/traffic/v1/incidents?key=Fmjtd|luuan1u2nh%2C2a%3Do5-96rw5u&callback=handleIncidentsResponse&boundingBox='.$a.','.$b.','.$c.','.$d.'&filters=construction,incidents&inFormat=kvp&outFormat=xml';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_feed_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);
$xml2 = new SimpleXMLElement($xml);
$xml2->asXML("test.xml");
$xml2->asXML();
$mysongs = simplexml_load_file("test.xml");
print_r($mysongs);
?>

Categories