I am making a call to some url : http://example.com/Service.asmx/getTodaysDiscussionForum which is xml data please see screenshot :
$response = file_get_contents('http://103.1.115.87:100/Service.asmx/getTodaysDiscussionForum');
$response = new SimpleXMLElement($response);
print_r($response);exit;
it display following output :
SimpleXMLElement Object (
[Table1] => Array ( [0] => SimpleXMLElement Object (
[Id] => 1210 [Title] => Test Discussion, Dont Reply [CreatedDate] => 4/25/2014 10:42:49 AM
[Status] => Not Sent )
[1] => SimpleXMLElement Object (
[Id] => 1182 [Title] => Negotiation Skills discussion [CreatedDate] => 4/25/2014 7:47:51 AM
[Status] => Not Sent )
)
)
How can I store each data in a variables ?
I am new to this xml reading thanks in advance
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
<?php
foreach($response[0] as $data) {
$id = $data->Id;
//...
}
<?php
$requestUrl = 'https://example/sitemap.xml';
$response = file_get_contents($requestUrl);
$responseXml = simplexml_load_string($response);
foreach ($responseXml->Table1 as $Table1) {
echo $Table1->Title;
echo "<br>";
}
?>
Related
How to display one element/object = LAT from this array and save it to a variable.
<?php
$url = "XML";
$xml = simplexml_load_file($url);
print_r($xml);
?>
//echo $xml->LAT;
//$value = (string) $xml->row[0]->LAT;
//echo $value;
Response fields XML:
SImpleXMLElement Object ( [row] => SimpleXMLElement Object ( [#attributes] => Array ( [MA] => 310627000 [LAT] => 9.967386 [LON] => 76.269330 ) ) )
I will be grateful for your help.
Problem solved.
echo $xml->row[0]['LAT'] . "<br>";
I am getting response from SMS API call like below
stdClass Object (
[balance] => 3
[batch_id] => 289728321
[cost] => 2
[num_messages] => 2
[message] => stdClass Object (
[num_parts] => 1
[sender] => TXTLCL
[content] => This is test message from abc
)
[receipt_url] =>
[custom] =>
[messages] => Array (
[0] => stdClass Object (
[id] => 1172603746 [recipient] => 919796736174 )
[1] => stdClass Object (
[id] => 1172603747 [recipient] => 919858566712)
)
[status] => success
)
The code which I am trying to tweak is like below
if(count($this->capturedResponse) > 0)
{
foreach($this->capturedResponse as $response)
{
$balance = $response[0];
$batch_id = $response[1];
...
}
}
I am not able to separate the stdClass Object fields separately and put them in their corresponding variables.
Please Help !!!
Please try like this
$balance = $response->balance;
$batch_id = $response->batch_id;
The easiest way is to JSON-encode your object and then decode it back to an array:
$capturedResponse = (object) (array(
'balance' => 1,
'batch_id' => 289728321,
'cost' => 2,
'num_messages' => 2,
'message' => (object) (array(
'num_parts' => 1
))
));
$array = json_decode(json_encode($capturedResponse), True);
echo $array['balance'];
You need to use -> to get element from object, so use like this
$balance = $response->balance;
$batch_id = $response->batch_id;
Instead
$balance = $response[0];
$batch_id = $response[1];
...
To get content of message
$message = $response->message->content
Try yourself for messages by loop let us know if any problem
Update
$messagesObj = $response->messages;
$messagesArr = array();
foreach($messagesObj as $key=>$value){
$messagesArr[] = $value->recipient;
}
$messages = implode(",",$messagesArr);
You will get 919796736174, 919858566712 in $messages
i am trying to select inserted data from orient db database and to echo it out ?
how can i do this using php ?
This is what i have done so far
require('OrientDB/OrientDB.php');
$db = new OrientDB('localhost', 2424);
$connected = $db->connect('root', 'hello');
$config = $db->DBOpen('tabe', 'root', 'hello');
if($connected)
{
$records = $db->query("select from Nation");
echo $records;
}
else
{
die('Server Error');
}
it just prints 'Array' on screen
i am running a Apache php server on windows machine.
how can i get the data from the databse and print in on the website.
when i use for each loop to print out the element
my script just time out
$records = $db->query("select from Nation");
foreach ($records as $v)
echo $v;
if i use print_r($records);
it gives the following output
Array ( [0] => OrientDBRecord Object ( [isParsed:OrientDBRecord:private] => [className:OrientDBRecord:private] => [type] => d [clusterID:OrientDBRecord:private] => 169 [recordPos:OrientDBRecord:private] => 0 [recordID:OrientDBRecord:private] => 169:0 [version] => 4 [content:OrientDBRecord:private] => Nation#in_partOf:%AQAAAAIAwQAAAAAAAAAAAMIAAAAAAAAAAA==;,Country_Name:"India",Iso_code:"IN" [data] => OrientDBData Object ( [data:OrientDBData:private] => Array ( ) [record:OrientDBData:private] => OrientDBRecord Object *RECURSION* ) ) [1] => OrientDBRecord Object ( [isParsed:OrientDBRecord:private] => [className:OrientDBRecord:private] => [type] => d [clusterID:OrientDBRecord:private] => 170 [recordPos:OrientDBRecord:private] => 0 [recordID:OrientDBRecord:private] => 170:0 [version] => 4 [content:OrientDBRecord:private] => Nation#Country_Name:"United States of America",Iso_code:"US" [data] => OrientDBData Object ( [data:OrientDBData:private] => Array ( ) [record:OrientDBData:private] => OrientDBRecord Object *RECURSION* ) ) [2] => OrientDBRecord Object ( [isParsed:OrientDBRecord:private] => [className:OrientDBRecord:private] => [type] => d [clusterID:OrientDBRecord:private] => 171 [recordPos:OrientDBRecord:private] => 0 [recordID:OrientDBRecord:private] => 171:0 [version] => 2 [content:OrientDBRecord:private] => Nation#Country_Name:"Canada",Iso_code:"CA" [data] => OrientDBData Object ( [data:OrientDBData:private] => Array ( ) [record:OrientDBData:private] => OrientDBRecord Object *RECURSION* ) ) )
how to convert it in to suitable form . like taking only names from it
this is my table
Better use PhpOrient Library it is the officially supported library by orient db
$client = new PhpOrient();
$client->hostname = 'localhost';
$client->port = 2424;
$client->username = 'your_username';
$client->password = 'your_password';
$client->connect();
$client->dbOpen('Your_db_name');
$query = "SELECT FROM NATION";
$result = $client->query($query);
$result = json_encode($result);
$result = json_decode($result);
foreach ($result as $row){
echo $row->oData->Country_Name;
echo $row->oData->Iso_code;
}
phporient : https://github.com/orientechnologies/PhpOrient.git
Print the result. Instead of echo
print_r($records);
In order to iterate over the result, either use foreach or for
First check if we have some results, using foreach as :
if(count($records) > 0){//check if we have a result
foreach($records as $value){//loop
print_r($value);
}
}
According to documentation i should not have magic quotes, but i do. I am building a website with the slim framework. I am trying to make a xml creator for my rss feed and the api call looks like this:
function createRSS($dbh, $args) {
$xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
$xml = $xml."<rss version=\"2.0\">\n";
$xml = $xml."\t<channel>\n";
$xml = $xml."\t\t<title>".$args['title']."</title>\n";
$xml = $xml."\t\t<link>".$args['link']."</link>\n";
$xml = $xml."\t\t<image>\n";
$xml = $xml."\t\t\t<url>".$args['imageURL']."</url>\n";
$xml = $xml."\t\t\t<title>".$args['imageTitle']."</title>\n";
$xml = $xml."\t\t\t<link>".$args['link']."</link>\n";
$xml = $xml."\t\t</image>\n";
$xml = $xml."\t\t<description>".$args['description']."</description>\n";
$xml = $xml."\t\t<language>".$args['language']."</language>\n";
$xml = $xml."\t\t<category>".$args['category']."</category>\n";
$xml = $xml."\t\t<copyright>".$args['copyright']."</copyright>\n";
$xml = $xml."\t\t<lastBuildDate>".$args['lastBuildDate']."</lastBuildDate>\n";
$xml = $xml."\t\t<ttl>".$args['ttl']."</ttl>\n";
$xml = $xml."\t</channel>\n";
$xml = $xml."</rss>\n";
try {
$xml = simplexml_load_string($xml);
$result['xmlString'] = print_r($xml);
$result['args'] = $args;
$file_path = stripslashes("/_rss/_profile/_".$args['profile']."/rss_".$args['profile'].".xml");
$result['xmlLink'] = $file_path;
$result['test'] = '/hey/there';
return $result;
}
catch (Exception $e) {
$result['error'] = "1";
$result['message'] = $e->getMessage();
return $result;
}
}
$app->post('/api/createRSS', function() use ($dbh) {
$args['user'] = $_POST['user'];
$args['title'] = $_POST['title'];
$args['link'] = $_POST['link'];
$args['description'] = $_POST['description'];
$args['language'] = "en-us";
$args['copyright'] = $_POST['copyright'];
$args['creationDate'] = "11/03/1993";
$args['ttl'] = "340";
$args['imageURL'] = $_POST['imageURL'];
$args['imageTitle'] = $_POST['imageTitle'];
$args['category'] = $_POST['title'];
echo print_r($args);
echo json_encode(createRSS($dbh, $args));
});
The output of the api call looks like this:
Array
(
[user] => 1
[title] => 2
[link] => 3
[description] => 4
[language] => en-us
[copyright] => 5
[creationDate] => 11/03/1993
[ttl] => 340
[imageURL] =>
[imageTitle] => 8
[category] => 2
)
1SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 2.0
)
[channel] => SimpleXMLElement Object
(
[title] => 2
[link] => 3
[image] => SimpleXMLElement Object
(
[url] => SimpleXMLElement Object
(
)
[title] => 8
[link] => 3
)
[description] => 4
[language] => en-us
[category] => 2
[copyright] => 5
[lastBuildDate] => SimpleXMLElement Object
(
)
[ttl] => 340
)
)
{"xmlString":true,"args":
{"user":"1","title":"2","link":"3","description":"4",
"language":"en-us","copyright":"5","creationDate":"11\/03\/1993",
"ttl":"340","imageURL":null,"imageTitle":"8","category":"2"},
"xmlLink":"\/_rss\/_profile\/_\/rss_.xml","test":"\/hey\/there"}
The big problem is my link in the xmlLink has the added back slashes which wont let me navigate to the correct file. I have printed the phpInfo Response and it looks like:
PHP Version 5.5.9-1ubuntu4.14
I have no idea what is going on and everyone says this shouldnt be a prblem in 5.5. Thanks.
I want to fetch data from different servers Database and I need to shown in one Centralised server. So I have written a SELECT query in different locations and I am trying to fetch in one Centralised Server.
I am getting a JSON Array response.
But I tried displaying it in a HTML Table format, but I am unable to get the proper result.
Here is my Centralised server PHP code:
<?php
$arr = array (
'http://example.com/pristatus/send-curl.php',
'http://example2.com/pristatus/send-curl.php'
);
for ($i =0; $i<count($arr); $i++)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $arr[$i]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
if($output!="error"){
$data = json_decode($output);
}
echo "<pre>";
print_r($data);
curl_close($ch);
}
?>
Output I am getting:
Array
(
[0] => stdClass Object
(
[id] => 9
[status] => OK
[batch] => 119677
[location] => Hyderabad
[createdDT] => 2015-06-19 20:40:05
)
)
Array
(
[0] => stdClass Object
(
[id] => 1
[status] => OK
[batch] => 56339
[location] => Mumbai
[createdDT] => 2015-06-19 20:40:05
)
[1] => stdClass Object
(
[id] => 2
[status] => OK
[batch] => 56339
[location] => Mumbai
[createdDT] => 2015-06-19 20:40:05
)
)
Please suggest me how I can display Json response in a Table format.
you need to use foreach loop and than get value by like
foreach($array as $val)
$val->property
Example :
echo '<table>';
foreach($data as $key) {
echo '<tr>';
echo '<td>'.$key->{'id'}.'<td>';
echo '<td>'.$key->{'status'}.'</td>';
echo '<td>'.$key->{'batch'}.'</td>';
echo '<td>'.$key->{'location'}.'</td>';
echo '<td>'.$key->{'createdDT'}.'</td>';
echo '</tr>';
}
echo '</table>';