php - how to check if there is text within XML element - php

:) Let's say that i have that code:
<sample number="1">TEXT</sample>
but sometimes it could be
<sample number"1"/>
Q: How to check if it's self closed or not ? Or I want to check if it's there TEXT within element sample
Note: I'm using that way to retrieve XML doc:
$content = #file_get_contents($url);
$xml = new SimpleXMLElement($content);

You need to type cast the element to string, then check if it's empty or not.
Here's a quick example:
$test = simplexml_load_string("<test><elem test='12'><sub /><sub /></elem><elem test='12'>hi</elem><elem test='9' /><elem /></test>");
foreach($test as $elem){
echo "\n";
var_dump($elem);
if((string)$elem == '' && $elem->count() == 0)
echo 'Empty';
else
echo 'Full';
}
Will return:
object(SimpleXMLElement)#3 (2) {
["#attributes"]=>
array(1) {
["test"]=>
string(2) "12"
}
["sub"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#4 (0) {
}
[1]=>
object(SimpleXMLElement)#5 (0) {
}
}
}
Full
object(SimpleXMLElement)#5 (2) {
["#attributes"]=>
array(1) {
["test"]=>
string(2) "12"
}
[0]=>
string(2) "hi"
}
Full
object(SimpleXMLElement)#3 (1) {
["#attributes"]=>
array(1) {
["test"]=>
string(1) "9"
}
}
Empty
object(SimpleXMLElement)#5 (0) {
}
Empty

Related

Assigning specific element of multidimensional array in url in php

My goal is to get specific element/value from a multidimensional array and assign them in a URL in loop. I have already tried and was able to get elements in the array but this displays all elements. I only want to get specific, like nid and field_x values.
This is my link structure: http://localhost:8080/$nid/$field_x
Expected result:
http://localhost:8080/123/one
http://localhost:8080/789/three
This is my sample var_dump result
array(1) {
[0]=>
array(38) {
["nid"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
int(123)
}
}
["vid"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
int(456)
}
}
["field_x"]=>
array(1) {
[0]=>
array(4) {
["target_id"]=>
string(6) "One"
}
}
["field_y"]=>
array(1) {
[0]=>
array(4) {
["target_id"]=>
string(2) "Two"
}
}
}
[1]=>
array(38) {
["nid"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
int(789)
}
}
["vid"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
int(321)
}
}
["field_x"]=>
array(1) {
[0]=>
array(4) {
["target_id"]=>
string(6) "Three"
}
}
["field_y"]=>
array(1) {
[0]=>
array(4) {
["target_id"]=>
string(2) "Four"
}
}
}
}
You can use a foreach() and get the data using $values['nid'][0]['value'] or $values['field_x'][0]['target_id']:
foreach ($result as $values) {
$nid = $values['nid'][0]['value'];
$field_x = $values['field_x'][0]['target_id'];
echo "http://localhost:8080/$nid/$field_x" ;
}
Will outputs:
http://localhost:8080/123/One
http://localhost:8080/789/Three
You probably want to do something else than an echo, so you can create a new array:
$urls = [];
foreach ($result as $values) {
$nid = $values['nid'][0]['value'];
$field_x = $values['field_x'][0]['target_id'];
$urls[] = "http://localhost:8080/$nid/$field_x" ;
}
print_r($urls);

Output and use array elements from a REST Api call in php

I am calling a REST Api to get data using curl in php. It gives me the list of data/contents in the api in Array php format.
I was able to get single element value using $resultArray[0]['nid'][0]['value'];. But my goal is to get elements in all contents in the api.
Say I want to get the following elements in the nested arrays.
$resultArray[0]['nid'][0]['value'];
$resultArray[0]['vid'][0]['value'];
$resultArray[0]['cid'][0]['value'];
And use these values in a loop too.
I am trying to search how I can do it loop, and if anyone can provide sample code, that would be appreciated.
Update:
This is the sample result of var_dump:
array(1) {
[0]=>
array(38) {
["nid"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
int(1)
}
}
["vid"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
int(2)
}
}
["cid"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
int(3)
}
}
["field"]=>
array(1) {
[0]=>
array(4) {
["target_id"]=>
int(4)
}
}
}
[1]=>
array(38) {
["nid"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
int(11)
}
}
["vid"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
int(22)
}
}
["cid"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
int(33)
}
}
["field"]=>
array(1) {
[0]=>
array(4) {
["target_id"]=>
int(44)
}
}
}
[2]=>
array(38) {
["nid"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
int(111)
}
}
["vid"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
int(222)
}
}
["cid"]=>
array(1) {
[0]=>
array(1) {
["value"]=>
int(333)
}
}
["field"]=>
array(1) {
[0]=>
array(4) {
["target_id"]=>
int(444)
}
}
}
}
And I want to use these elements values in a loop.
Say my expected result is.
Test1 = "1", "2", "3"
Test2 = "11", "22", "33"
Test3 = "111", "222", "333"
These equivalent numbers should be comming from the element nid, vid, cid.
I dont just want to assign/echo these values in the result as I have array[100s] in one api call.
Since your array is a multidimensional array, you need to have nested foreach loops:
$test = 1;
foreach ($resultArray as $items) {
// Echo the current test number
echo "Test{$test} = ";
$values = [];
foreach ($items as $item) {
// Get the correct value.
if (array_key_exists('value', $item[0])) {
$values[] = $item[0]['value'];
continue;
}
if (array_key_exists('target_id', $item[0])) {
$values[] = $item[0]['target_id'];
continue;
}
}
echo '"' . implode('", "', $values) . '"' . "\n";
$test++;
}
Demo: https://3v4l.org/fnHlV

How to access XML response

Good day, I want to access the XML response and echo it to display its value but I don't know how to do it. I already tried some few answers in StackOverflow but I fail.
This is my code.
<?php
error_reporting(E_ALL);
require_once 'ruFunctions.php';
$rentalsUnited = new rentalsUnited();
$ru= $rentalsUnited->getOwners();
if($ru != null){
$data= simplexml_load_string($ru);
var_dump($data); // it will return boof(false)
var_dump($ru);
echo $data->Pull_ListAllOwners_RS->Status['ID']; //Trying to get property of non-object
}
?>
Results for var_dump($ru);
object(SimpleXMLElement)#2 (3) {
["Status"]=>
string(7) "Success"
["ResponseID"]=>
string(32) "44065d9888304e8cba912bce4d131ab1"
["Owners"]=>
object(SimpleXMLElement)#3 (1) {
["Owner"]=>
object(SimpleXMLElement)#4 (7) {
["#attributes"]=>
array(1) {
["OwnerID"]=>
string(6) "429335"
}
["FirstName"]=>
string(5) "Test"
["SurName"]=>
string(7) "Tester"
["CompanyName"]=>
string(15) "Test Helpers"
["Email"]=>
string(23) "info#Test.com"
["Phone"]=>
string(12) "+13474707707"
["UserAccountId"]=>
string(3) "602"
}
}
}
It looks like $ru is already a SimpleXMLElement, so trying to call simplexml_load_string will fail on this.
You can see some of the details by
if($ru != null){
echo $ru->Status;
}
You can (probably) list the owners by...
if($ru != null){
foreach ($ru->Owners->Owner as $owner ) {
echo "ownerId=".$owner['OwnerID'].PHP_EOL;
echo "FirstName=".$owner->FirstName.PHP_EOL;
}
}

How to get facebook album photos id and time using php?

I'm new in php. I make below two code to get facebook album photos id and create time. But its not working and don't showing me result/ID, only showing me blank page.
Here is my two code with $json and $array output.
Code 1;
<?php
$token="<token>";
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?fields=id&access_token=$token");
$json = json_decode($data);
echo $json->id;
echo $json->created_time;
?>
Code 1 Output: using var_dump($json);
object(stdClass)#1 (2) {
["data"]=>
array(3) {
[0]=>
object(stdClass)#2 (2) {
["id"]=>
string(15) "160246594547781"
["created_time"]=>
string(24) "2017-08-04T18:09:13+0000"
}
[1]=>
object(stdClass)#3 (2) {
["id"]=>
string(15) "160246581214449"
["created_time"]=>
string(24) "2017-08-04T18:09:12+0000"
}
[2]=>
object(stdClass)#4 (2) {
["id"]=>
string(15) "160246587881115"
["created_time"]=>
string(24) "2017-08-04T18:09:13+0000"
}
}
["paging"]=>
object(stdClass)#5 (1) {
["cursors"]=>
object(stdClass)#6 (2) {
["before"]=>
string(20) "MTYwMjQ2NTk0NTQ3Nzgx"
["after"]=>
string(20) "MTYwMjQ2NTg3ODgxMTE1"
}
}
}
Code 2:
<?php
$token="<token>";
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?fields=id&access_token=$token");
$array = json_decode($data, true);
echo $array['data']['id'];
echo $array['data']['created_time'];
?>
Code 2 Output: Using var_dump($array);
array(2) {
["data"]=>
array(3) {
[0]=>
array(2) {
["id"]=>
string(15) "160246594547781"
["created_time"]=>
string(24) "2017-08-04T18:09:13+0000"
}
[1]=>
array(2) {
["id"]=>
string(15) "160246581214449"
["created_time"]=>
string(24) "2017-08-04T18:09:12+0000"
}
[2]=>
array(2) {
["id"]=>
string(15) "160246587881115"
["created_time"]=>
string(24) "2017-08-04T18:09:13+0000"
}
}
["paging"]=>
array(1) {
["cursors"]=>
array(2) {
["before"]=>
string(20) "MTYwMjQ2NTk0NTQ3Nzgx"
["after"]=>
string(20) "MTYwMjQ2NTg3ODgxMTE1"
}
}
}
Please help me to solve this issue.thanks
For first one you need to do:-
<?php
$token="<token>";
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?fields=id&access_token=$token");
$json = json_decode($data);
foreach($array.data as $arr){
echo $arr.id; echo PHP_EOL; // you can use `echo "<br/>";`
echo $arr.created_time;echo PHP_EOL;// you can use `echo "<br/>";`
}
?>
For second-one you can use:-
<?php
$token="<token>";
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?
fields=id&access_token=$token");
$array = json_decode($data, true);
foreach($array['data'] as $arr){
echo $arr['id']; echo PHP_EOL; // you can use `echo "<br/>";`
echo $arr['created_time'];echo PHP_EOL;// you can use `echo "<br/>";`
}
?>
Output:- https://eval.in/841721

Recursive JSON transformation

Suppose I have a JSON string:
$json = '{"lemon":"test",
"orange":["toto", "tata", "tete"],
"zob":[{"id":"0"}, {"id":"1"}]}';
I'd like to cycle through that encoded object to modify every string in it, so I have a recursive function:
function doObject($__obj){
$__obj = cycleObject($__obj);
return $__obj;
}
function cycleObject($__obj){
$type = gettype($__obj);
foreach($__obj as $var => &$val){
switch(gettype($val)){
case 'object':
cycleObject($val);
break;
case 'array':
cycleObject($val);
break;
case 'string':
if($type == 'object'){
$__obj->$var = $val.'-ok';
}else{
if($type == 'array'){
$__obj[$var] = $val.'-ok';
}
}
break;
}
}
return $__obj;
}
And I call the function:
$obj = doObject(json_decode($json));
var_dump($obj);
Which gives :
object(stdClass)#1 (3) {
["lemon"]=> string(7) "test-ok"
["orange"]=> array(3) {
[0]=> string(4) "toto"
[1]=> string(4) "tata"
[2]=> string(4) "tete" }
["zob"]=> array(2) {
[0]=> object(stdClass)#2 (1) {
["id"]=> string(4) "0-ok" }
[1]=> object(stdClass)#3 (1) {
["id"]=> string(4) "1-ok" }
}
}
Now my problem is, for some reason, I am unable to modify directly inside an array composed by string, or should I say, the modified string inside an array (and not inside an object inside an array) because the array loses its reference. How do I fix that so in orange I instead obtain:
[0]=> string(7) "toto-ok"
[1]=> string(7) "tata-ok"
[2]=> string(7) "tete-ok"
Your array of strings isn't being scrutinized correctly by your function. Basically, in each array you need a second check to see if you are dealing with another array/object or a string, otherwise regular arrays of strings are being bypassed....oddly enough. The following should work for you:
$json = '{"lemon":"test",
"orange":["toto", "tata", "tete"],
"zob":[{"id":"0"}, {"id":"1"}]}';
function doObject($__obj){
$__obj = cycleObject($__obj);
return $__obj;
}
function cycleObject($__obj){
foreach($__obj as $key => &$val){
if(is_object($val)) {
cycleObject($val);
}
if(is_array($val)) {
foreach($val as &$v) {
if(is_object($v) || is_array($v)) {
cycleObject($v);
} else {
$v .= '-ok';
}
}
}
if(is_string($val)) {
$val .= '-ok';
}
}
return $__obj;
}
$obj = doObject(json_decode($json));
var_dump($obj);
This produced the results you were looking for in my local environment.
object(stdClass)#1 (3) {
["lemon"]=>
string(7) "test-ok"
["orange"]=>
array(3) {
[0]=>
string(7) "toto-ok"
[1]=>
string(7) "tata-ok"
[2]=>
string(7) "tete-ok"
}
["zob"]=>
array(2) {
[0]=>
object(stdClass)#2 (1) {
["id"]=>
string(4) "0-ok"
}
[1]=>
object(stdClass)#3 (1) {
["id"]=>
string(4) "1-ok"
}
}
}

Categories