Can you help me there is a problem that is detect by php when execute but I think my code is okay.
The error say "Catchable fatal error: Object of class stdClass could not be converted to string in C:\Program Files\wamp\www\getkongregate.php on line 46"
Website of kongregate xml: here
This is the code I have:
<?php
$xml = simplexml_load_string(file_get_contents('http://www.kongregate.com/games_for_your_site.xml'));
$json = json_encode($xml);
$games = json_decode($json);
$count = 0;
foreach ($games->game as $game) {
echo $game->id . "\n";
echo $game->title . "\n";
echo $game->thumbnail . "\n";
echo $game->launch_date . "\n";
echo $game->category . "\n";
if (array_key_exists('screenshot',$game)) {
for($i=0;$i<=(count($game->screenshot)-1);$i++) {
echo $game->screenshot[$i] . "\n";
}
}
echo $game->flash_file . "\n";
echo $game->width . "\n";
echo $game->height . "\n";
echo $game->description . "\n";
echo $game->instructions . "\n";
echo $game->gameplays . "\n";
echo $game->rating . "\n\n------\n\n";
if ($count == 100) {break;}
$count++;
}
?>
You are retreiving xml.
Why are you parsing it as json?
Use SimpleXmlElement instead and then iterate through xml elements to retreive your data.
$xml = file_get_contents($map_url);
$simpleXmlElement = simplexml_load_string($xml);
var_dump($simpleXmlElement);
Here: :)
$url = 'http://www.kongregate.com/games_for_your_site.xml';
$content = file_get_contents($url)
$xml = simplexml_load_string($content);
$count = 0;
foreach ($xml as $game) {
echo strval($game->id) . PHP_EOL;
echo strval($game->title) . PHP_EOL;
echo strval($game->thumbnail) . PHP_EOL;
echo strval($game->launch_date) . PHP_EOL;
echo strval($game->category) . PHP_EOL;
if (isset($game->screenshot) && is_array($game->screenshot)) {
foreach ($game->screenshot as $screenshot) {
echo strval($screenshot) . "\n";
}
}
echo strval($game->flash_file) . PHP_EOL;
echo strval($game->width) . PHP_EOL;
echo strval($game->height) . PHP_EOL;
echo strval($game->description) . PHP_EOL;
echo strval($game->instructions) . PHP_EOL;
echo strval($game->gameplays) . PHP_EOL;
echo strval($game->rating) . PHP_EOL;
if ($count >= 100) {
break;
}
$count++;
}
Related
The code snippet below generates '12290' from the second echo output but the third one is still zero.
How do I get a proper return value from mytest() function?
$testId = 0;
echo 'first: ' . $testId . '<br>';
function mytest($postId) {
if (get_post_type($postId) === 'artist') {
$testId = $postId;
}
echo 'second: ' . $testId . '<br>';
return $testId;
}
mytest(12290);
echo 'third: ' . $testId . '<br>';
You forgot to assign the return value to the variable again.
It should be this:
$testId = 0;
echo 'first: ' . $testId . '<br>';
function mytest($postId) {
if (get_post_type($postId) === 'artist') {
$testId = $postId;
}
echo 'second: ' . $testId . '<br>';
return $testId;
}
$testId = mytest(12290);
echo 'third: ' . $testId . '<br>';
Instead of:
mytest(12290);
echo 'third: ' . $testId . '<br>';
Try:
echo 'third: ' . mytest(12290) . '<br>';
I want to get the episode number and Released from
http://www.omdbapi.com/?t=the+walking+dead&season=6&r=json
but I don't know how to extract arrays from the api.
Can someone help me?
My code is:
$title = 'the+walking+dead';
$title2 = urlencode($title);
$json=file_get_contents("http://www.omdbapi.com/?t=$title2&season=6&r=json");
$details=json_decode($json);
if($details->Response=='True')
{
$episodios=$details->Episodes;
}
$title = 'the+walking+dead';
$title2 = urlencode($title);
$json = file_get_contents("http://www.omdbapi.com/?t=$title2&season=6&r=json");
$details = json_decode($json);
if ($details->Response == 'True') {
echo 'There are ' . count($details->Episodes) . ' episodes<br />';
foreach ($details->Episodes as $key => $episode) {
echo 'Episode criteria number is ' . ($key + 1) . '<br />';
echo 'Episode Number: ' . $episode->Episode . '<br />';
echo 'Released: ' . $episode->Released . '<br />';
}
}
<?php
$title = 'the+walking+dead';
$title2 = urlencode($title);
$json=file_get_contents("http://www.omdbapi.com/t=$title2&season=6&r=json");
$details=json_decode($json);
if($details->Response=='True')
{
$episodios=$details->Episodes;
foreach ($episodios as $episode) {
echo 'Episode Number: ' . $episode->Episode . '<br />';
echo 'Released Date: ' . $episode->Released . '<br />';
}
}
?>
<?php
$xml = simplexml_load_file("sample.xml");
echo " ". $xml->getName() . "<br />";
foreach($xml->children()->children() as $child)
{
$a=$child->getName() . "<br />";
array_push($a);
echo $a;
}
?>
Try this
<?php
$arr = array();
$xml = simplexml_load_file("sample.xml");
echo " ". $xml->getName() . "<br />";
foreach($xml->children()->children() as $child)
{
$a=$child->getName() . "<br />";
$arr[] = $a;
echo $a;
}
?>
I am getting the result from the SOAP client as an response. I just have to analyze the structure of the parameter and display the results accordingly. I have the following srucutre for SerialEquipment and i am getting the results peoperly for all the parameters except the Esaco parameter. The Esaco parameter is an array object and it resides inside the SerialEquipment array. I am trying to fetch the response from Esaco array object but getting an error as Invalid arguments supplied for foreach. I am not understanding how to get the results for Esaco Parameter by looping properly.Just a small mistake i am doing in looping the array.
Code:
foreach($Serial as $key => $obj)
{
echo "<b>"."Serial Equipment=>" . $key . "</b>"."<br>";
echo "Code=>". $obj->Code . "<br>";
echo "Desc Short=>". $obj->Desc_Short . "<br>";
echo "Desc Long=>". $obj->Desc_Long . "<br>";
foreach($obj->Esaco as $key2 => $obj2)
{
if($obj2 === null){
// doesn't contain Esaco
break;
}
else{
echo "<b>"."Esaco=>" . $key2 . "</b>"."<br>";
echo "EsacoMainGroupCode=>". $obj2->EsacoMainGroupCode . "<br>";
echo "EsacoMainGroupDesc=>". $obj2->EsacoMainGroupDesc . "<br>";
echo "EsacoSubGroupCode=>". $obj2->EsacoSubGroupCode . "<br>";
echo "EsacoSubGroupCode=>". $obj2->EsacoSubGroupDesc . "<br>";
echo "EsacoSubGroupCode=>". $obj2->EsacoGroupCode . "<br>";
echo "EsacoSubGroupCode=>". $obj2->EsacoGroupDesc . "<br>";
}
}
}
if($parameter['aktion'] == 'getVehicle')
{
$vehicle = getVehicleValuation();
$Serial=$vehicle['SerialEquipment'];
$VehicleFuel=$vehicle['VehicleFuel'];
foreach($VehicleFuel as $key => $obj2)
{
echo "Fuel Type=>". $obj2->Fuel_Type . "<br>";
echo "Fuel Type Code=>". $obj2->Fuel_Type_Code . "<br>";
echo "ECE_Unit=>". $obj2->ECE_Unit . "<br>";
echo "ECE_In=>". $obj2->ECE_In . "<br>";
echo "ECE_Out=>". $obj2->ECE_Out . "<br>";
echo "ECE_All=>". $obj2->ECE_All . "<br>";
echo "ECE_CO2=>". $obj2->ECE_CO2 . "<br>";
}
}
This is my structure for SerialEquipment:
if($parameter['aktion'] == 'getVehicle')
{
$vehicle = getVehicleValuation();
if(($serials = $vehicle['SerialEquipment']) === null){
// doesn't contain SerialEquipment
break;
}
foreach($serials as $serial){
print "Code =>" . $serial->Code . "<br>";
print "Desc Short =>" . $serial->Desc_Short . "<br>";
//...
foreach($serial->Esaco as $esaco){
print "EsacoMainGroupCode =>" . $esaco->EsacoMainGroupCode. "<br>";
print "EsacoMainGroupDesc =>" . $esaco->EsacoMainGroupDesc. "<br>";
//...
}
}
}
And for the VehicleFuel:
if($parameter['aktion'] == 'getVehicle')
{
$vehicle = getVehicleValuation();
$Serial=$vehicle['SerialEquipment'];
$VehicleFuel=$vehicle['VehicleFuel'];
$fuelType = $VehicleFuel->Fuel_Type;
// if there is only going to be one VehicleFuel object Vehicle, then just do..
echo "Fuel Type =>". fuelType->Fuel_Type . "<br>";
echo "Fuel Type Code =>". $fuelType->Fuel_Type_Code . "<br>";
// if there will be more than one, you will want to use a loop...
foreach($fuelType as $obj)
{
echo "Fuel Type=>". $obj->Fuel_Type . "<br>";
echo "Fuel Type Code=>". $obj->Fuel_Type_Code . "<br>";
echo "ECE_Unit=>". $obj->ECE_Unit . "<br>";
echo "ECE_In=>". $obj->ECE_In . "<br>";
echo "ECE_Out=>". $obj->ECE_Out . "<br>";
echo "ECE_All=>". $obj->ECE_All . "<br>";
echo "ECE_CO2=>". $obj->ECE_CO2 . "<br>";
}
}
The key Esaco is an object not an array. You should change your second foreach.
foreach($Serial as $key => $obj) {
echo "Serial Equipment=>" . $key . "<br>";
echo "Code=>". $obj->Code . "<br>";
echo "Desc Short=>". $obj->Desc_Short . "<br>";
echo "Desc Long=>". $obj->Desc_Long . "<br>";
foreach($obj->Esaco as $key2 => $obj2) {
echo $obj2;
//...
}
}
I'm trying to show a list of all nearby weather stations.
I have the code:
$json_string = file_get_contents("http://api.wunderground.com/api/8b19ccf6a06c0826/geolookup/conditions/q/Netherlands/Rotterdam.json");
$parsed_json = json_decode($json_string);
$stations = $parsed_json->{'location'}->{'nearby_weather_stations'}->{'pws'}->{'station'};
$count = count($stations);
for($i = 0; $i < $count; $i++)
{
$station = $stations[$i];
if (count($station) > 1)
{
echo "City: " . $station->{'city'} . "\n";
echo "State: " . $station->{'state'} . "\n";
echo "Latitude: " . $station->{'lat'} . "\n";
echo "Longitude: " . $station->{'lon'} . "\n";
}
}
But currently it's not showing anything, i have searched for examples but i couldn't find any solution fot this problem.
Alternatively, you could use a simple foreach to iterate those values. Consider this example:
$json_string = file_get_contents("http://api.wunderground.com/api/8b19ccf6a06c0826/geolookup/conditions/q/Netherlands/Rotterdam.json");
$parsed_json = json_decode($json_string, true); // <-- second parameter to TRUE to use it as an array
$desired_values = $parsed_json['location']['nearby_weather_stations']['pws']['station'];
foreach($desired_values as $key => $value) {
echo "<hr/>";
echo "City: " . $value['city'] . "<br/>";
echo "State: " . $value['state'] . "<br/>";
echo "Latitude: " . $value['lat'] . "<br/>";
echo "Longitude: " . $value['lon'] . "<br/>";
echo "<hr/>";
}
Sample Fiddle