Get XML element by tag name - php

var_dump($object) outputs the following result:
object(stdClass)#9 (5) {
["data"]=> object(stdClass)#8 {
["validFiling"]=> object(stdClass)#7 {
["indicators"]=> string(6) "MODE_S"
}
["plan"]=> object(stdClass)#6 {
["id"]=> string(10) "xxx"
}
}
}
In this data structure I need to access the content of the field id. I do this in the following way:
try
{
$object = $client->getPlan($p);
var_dump($object);
}
catch (Exception $e) {
print $e->getMessage();
}
$line = $client->getLastResponse();
$doc = new DOMDocument();
$doc->loadXML($line);
$data = $doc->getElementsByTagName('data');
$fp = $data->getElementsByTagName('plan');
$id = $fp->getElementsByTagName('id');
$fId = $id->item(0)->nodeValue;
And the error is (at the line $fp = $data->getElementsByTagName('plan')):
Call to undefined method DOMNodeList::getElementsByTagName()
How to solve this issue?

The error occurs because $data is a DOMNodeList which doesn't have the getElementsByTagName() method. You have the same problem with the $fp variable. If you want to access the first data element found, then change to:
$data = $doc->getElementsByTagName('data')->item(0);
$fp = $data->getElementsByTagName('plan')->item(0);
$id = $fp->getElementsByTagName('id');
Or if you want to iterate of the data elements and apply the processing to each:
$dataList = $doc->getElementsByTagName('data');
foreach($dataList as $data)
{
$fp = $data->getElementsByTagName('plan')->item(0);
$id = $fp->getElementsByTagName('id');
$fId = $id->item(0)->nodeValue;
}
The above works because its calling getElementsByTagName() on the node itself not the list.

Related

I can't get url's html content which is in array with DOMXpath

function pageContent(String $url): \DOMDocument
{
$html = cache()->rememberForever($url, function () use ($url) {
return file_get_contents($url);
});
$parser = new \DOMDocument();
libxml_use_internal_errors(true);
$parser->loadHTML($html);
libxml_use_internal_errors(false);
return $parser;
}
$linkBox is holding a website array which is like this:
array(3) {
[0]=>
string(34) "https://lions-mansion.jp/MF161026/"
[1]=>
string(34) "https://lions-mansion.jp/MF171045/"
[2]=>
string(34) "https://lions-mansion.jp/MF171010/"
}
So with foreach I try to go inside the links
but I couldn't.
foreach ($linkBox as $box){
$content = pageContent($box);
foreach ($content as $xpathgo){
$Pars = new \DOMXPath($xpathgo);
$Route = $Pars->query("//ul[#id='snav']/li/a");
foreach ($Route as $Rot){
$get = $Rot->getAttribute('href');
}
}
}
var_dump($get);
It returns nothing.
Any idea, what the problem is? Thank you for helping!

How to create the query to mongodb from Codeigniter?

I have such collection:
http://prntscr.com/fj87n5
If we open one document we will see
http://prntscr.com/fj882i
As you see driver is an object, I trying to get all documents in which driver.id = $driver_id:
$driver_id = $this->input->post('driver_id');
$driverRides = $this->app_model->get_selected_fields(RIDES, array('driver.id' => $driver_id), array('history.end_ride'));
var_dump($driverRides->num_rows());
foreach($driverRides->row() as $val){
var_dump($val);
}
die;
I see 57 rows (while the collection contains 50 documents). And I didn't see all 50 documents (there is 50 documents with the same driver) in output.
I see this response:
int(57)
object(MongoId)#537 (1) {
["$id"]=>
string(24) "58e5ebcc835fe4c902a698e4"
}
array(1) {
["end_ride"]=>
object(MongoDate)#538 (2) {
["sec"]=>
int(1491463147)
["usec"]=>
int(0)
}
}
I think I have see 50 different end_ride outputs. Why is this so?
try{
$ConnMongo = new MongoClient("mongodb://192.129.1.121/");
$mdata = $ConnMongo->abc->xyz;
}
catch(MongoConnectionException $e){
exit('START THE MONGO SERVER PLZ ...!!!');
}
$data = $mdata->find(array('driver_id'=>$driver_id));
echo $datacount = $data->count();
foreach ( $data as $id)
{
echo '<pre>';
print_r($id);
}
}

PrestaShop: Get category name from webservice

I'm trying to get the category name from PrestaShop API.
But I'm failing to parse this xml object. I want to get the value "Root", but I don't know how to achieve this. From the PrestaShop data, I can get this data, how can I get the "Root" value?
object(SimpleXMLElement)#5 (2) { ["#attributes"]=> array(1) { ["id"]=> string(1) "2" } [0]=> string(4) "Root" }
This is my code:
$store_path = 'http://xxxx.com/';
$auth_key = 'xxxxxx';
$webService = new PrestaShopWebservice($store_path, $auth_key, false);
// The key-value array
$opt['resource'] = 'categories';
$opt['display'] = 'full';
$opt['limit'] = 10;
//Retrieving the XML data
$xml = $webService->get($opt);
var_dump($xml[0]->categories[0]->category->name->language[0][0]);
Try this:
$resource = $xml->categories->children();
echo $resource->category->name->language[0]->__toString();

PHP Trying to get property of non-object for valid object

I have the following problem:
I'm iterating through an array of valid objects using foreach. When trying to access the resulting objects or their properties I am getting the notice I would be trying to access a non-object.
Here is the code:
$schema = json_decode($_POST['d']);
foreach ($schema->node as $node) {
var_dump($node);
if ($node->status == 1) {
$data = $node->id;
}
}
var_dump outputs the following:
object(stdClass)#5 (6) {
["status"]=>
int(0)
["id"]=>
int(1)
["title"]=>
string(6) "Sensor"
["script"]=>
string(24) "from eZness import swag;"
["x"]=>
int(60)
["y"]=>
int(80)
}
Thanks in advance.
UPDATE:
$schema = json_decode($_POST['d']);
foreach ($schema->node as $node) {
var_dump($node);
echo $node->status; //Funnily this works
$status = $node->status; //while this doesn't
if ($node->status == 1) { //and this doesn't as well
$data = $node->id;
}
}
But when removing the var_dump even the echo doesn't work anymore.
UPDATE:
Resolved. Had a look at the client part of the application, there was a problem with pushing NULL values in the $schema->node array which of course are non-objects.
You are trying to access $node->data, which does not exist.
Perhaps more of a workaround than an answer but: use
$schema = json_decode($_POST['d'],true);
When you pass true as the second parameter, you get back an associative array instead of an object.
You should be able to loop through it with this:
$schema = json_decode($_POST['d'],true);
foreach ($schema['node'] as $node) {
if ($node['status'] == 1) {
$data = $node['id'];
}
}

PHP/JSON - how to parsing before import to mysql

i want to save some items to database from json.
json source is here:
http://api.worldoftanks.eu/2.0/account/tanks/?application_id=d0a293dc77667c9328783d489c8cef73&account_id=500014703
I want to save the database only those values
"tank_id":
"battles":
"wins":
from each section
for checking JSON source i using this part of script, work fine
function readWotResponse($url, $isUrl = true)
{
if ($isUrl && !ini_get('allow_url_fopen')) {
throw new Exception('allow_url_fopen = Off');
}
$content = file_get_contents($url);
if ($isUrl) {
$status_code = substr($http_response_header[0], 9, 3);
if ($status_code != 200) {
throw new Exception('Got status code ' . $status_code . ' expected 200');
}
}
$json = json_decode($content);
if (!$json) {
throw new Exception('Response contained an invalid JSON response');
}
if ($json->status != 'ok') {
throw new Exception($json->status_code);
}
return $json;
}
for parsing i try
$tanks_id_url = "http://api.worldoftanks.eu/2.0/account/tanks/?application_id=d0a293dc77667c9328783d489c8cef73&account_id=500014703";
try {
$tjson = readWotResponse($tanks_id_url);
print_r ($tjson) ;
}
so and now i need help with good parsing to got good value.
when i use var_dump i got this
object(stdClass)#1 (3) { ["status"]=> string(2) "ok" ["count"]=>
int(1) ["data"]=> object(stdClass)#2 (1) {
["500014703"]=>
array(106) {
[0]=>
object(stdClass)#3 (6) {
["achievements"]=>
object(stdClass)#4 (59) {
["medal_dumitru"]=>
int(0) . . . . .
this part is problem for me
**array(106) {
[0]=>**
because when i want use
$value = $tjson->data->500014703->O->achievements->medal_dumitru;
i got error :-(

Categories