Reading XML POST data from URL - php

I am working with a 3rd party SMS supplier which they are sending me the delivery report of the SMS via URL as below:
http://www.mydomain.com/dlr.php <DeliveryReport><message id="024042313063119191" sentdate="2014/04/23 15:06:31" donedate="2014/04/23 15:06:35" status="DELIVERED" gsmerror="0" price="7.0" /></DeliveryReport>
And i am trying to read the XML data in dlr.php like below:
<?php
// read raw POST data
$postData = file_get_contents("php://input");
$dom = new DOMDocument();
$dom->loadXML($postData);
// create new XPath object for quering XML elements (nodes)
$xPath = new domxpath($dom);
// query “message” element
$reports = $xPath->query("/DeliveryReport/message");
// write out attributes of each “message” element
foreach ($reports as $node) {
echo “<br>id: “ . $node->getAttribute('id');
echo “<br>sent: “ . $node->getAttribute('sentdate');
echo “<br>done: “ . $node->getAttribute('donedate');
echo “<br>status: “ . $node->getAttribute('status');
echo “<br>gsmerrorcode: “ . $node->getAttribute('gsmerrorcode');
}
?>
I am getting this error:
Warning: DOMDocument::loadXML(): Empty string supplied as input in dlr.php
Any help how can I read the posted data correctly.
Thanks,

You can simply use this function for getting data from XML
function getFeed($feed_url)
{
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
foreach($x->channel->item as $entry) : ?>
<?php
$pdate = $entry->pubDate;
$pdate = rtrim($pdate,' -500');
$pdate = explode(', ',$pdate);
?>
<div >
<a href="<?php echo $entry->link; ?>" target="_blank">
<span > <?php echo $entry->title;?></span></a> <?php echo $pdate[1]; ?>
</div>
<?php
endforeach;
}
getFeed("// Your URL");

Related

php - get a link's value

I'm using php to get a part of a html file:
HTML file:
<div class="titles">
<h2>First Title</h2>
</div>
PHP file:
<?php
include_once('simple_html_dom.php');
$url = 'http://example.com';
$html = file_get_html($url);
$titles = $html->find('.titles');
$heading = $titles->find('h2')[0];
$link = $heading->find('a')[0];
echo $link;
//result: First Title
?>
How can I separately get the value of href and 'a' tag?
Because I want to save the title and link into the database,
I need '#' and 'First Title' not the 'a' tag.
$link should be a Simple HTML Element object, of which you can access attributes using $link->href and the text contents as $link->plaintext. See http://simplehtmldom.sourceforge.net/manual.htm.
U can use DOMDocument and DOMXpath object's (>=php5)
ref: http://php.net/manual/en/class.domdocument.php
part of sample code:
$html = '<div class="titles">
<h2>First Title</h2>
</div>';
$page = new DOMDocument();
$page->loadHtml($html);
$xpath = new DOMXpath($page);
$a = $xpath->query("//a");
for ($i=0; $i < $a->length; $i++) {
$_a = $a->item($i);
echo $_a->getAttribute("href");
echo "<br>";
echo $_a->textContent;
}

Convert form request data to xml in cakephp

I'm trying to convert all the form data in a cakephp request object to xml and then convert that to a string so that I can place it in a (blob) column in a mysql table.
I'm trying to do this current using the buildin xml builders in CakePHP 2.x.x as shown below but am getting an error.
if ($this->request->is('post')) {
$this->Survey->create();
$xml = Xml::build($this->request->data);
}
The form is shown below
<?php echo $this->Form->create('Survey'); ?>
<fieldset>
<legend><?php echo __('Add Survey'); ?></legend>
<?php
echo $this->Form->input('Question 1');
echo $this->Form->input('Question 2');
echo $this->Form->input('Question 3');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
The error I'm getting seems to be due to the DOCDocument->createElement(string,string) in the stacktrace. I've also used other methods including building it manually like so:
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$doc->loadHTML($this->request->data);
$data = $this->request->input('Xml::build',
array('return' => 'domdocument'));
while(list($key,$value) = each($this->request->data)){
$data = $data . $key . $value;
}
if(isset($this->request->data)){
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$root = $doc->appendChild($doc->createElement('survey'));
$post = $this->request->data['Survey'];
unset($post['submit']);
foreach($post as $key => $value){
$node = $doc->createElement($key,$value);
$root->appendChild($node);
}
$test1 = $doc->saveXML();
Any help would be appreciated. Thank you.
See the transforming an array into a string of XML section.

Trying to read values from an XML feed and display them in PHP

I'm trying to display values from this xml feed:
http://www.scorespro.com/rss/live-soccer.xml
In my PHP code I have the following loop but it does not display the results on my page:
<?php
$xml = simplexml_load_file("http://www.scorespro.com/rss/live-soccer.xml");
echo $xml->getName() . "<br>";
foreach($xml->children() as $item)
{
echo $item->getName() . ": " . $item->name . "<br>";
}
?>
For some reason it only shows:
rss
channel:
I'm fairly new to how XML works so any help would be much appreciated.
You can get actual data from $xml->channel->item so use like below
$items = $xml->channel->item;
foreach($items as $item) {
$title = $item->title;
$link = $item->link;
$pubDate = $item->pubDate;
$description = $item->description;
}
DEMO.
you can use below code
$dom = new DOMDocument;
$dom->loadXML($url);
if (!$dom) {
echo 'Error while parsing the document';
exit;
}
$xml = simplexml_import_dom($dom);
$data = $xml->channel->item;

echo xml with tags on html

I am trying to echo xml and I cant mange to echo the xml tags
,this is my code
<?php
// set feed URL
$feedURL = 'http://gdata.youtube.com/feeds/api/videos?orderby=updated&vq=rihana';
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
?>
<h1><?php echo $sxml->title; ?></h1>
<?php
$xml = new SimpleXMLElement('<xml></xml>');
// iterate over entries in feed
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get video player URL
$attrs = $media->group->player->attributes();
$watch = $attrs['url'];
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$thumbnail = $attrs['url'];
$track = $xml->addChild('item');
$track->addChild('description', $media->group->description);
$track->addChild('title', $media->group->title);
$track->addChild('url', $watch);
}
?>
<p>
<?php
header('Content-type: text/xml');
echo( htmlentities( $xml->asXML() ) );
foreach ($xml->item as $record) {
echo '<div class="item_record">'."\n";
echo '<h3>'.$record->title.'</h3>'."\n";
echo '<p><span class="category">description: </span>'.$record->description.'</p>'."\n";
echo '<p><span class="category">url: </span><a href='.$record->url.'>'.$record->url.'</a></p>'."\n";
echo '</div>'."\n";
}?>
</p>
i really want to print the data in xml format its like a mix from the code below
echo( htmlentities( $xml->asXML() ) );
and look nice like
foreach ($xml->item as $record) {
echo '<div class="item_record">'."\n";
echo '<h3>'.$record->title.'</h3>'."\n";
echo '<p><span class="category">description: </span>'.$record->description.'</p>'."\n";
echo '<p><span class="category">url: </span><a href='.$record->url.'>'.$record->url.'</a></p>'."\n";
echo '</div>'."\n";
}?>
which operation is the best to solve my problem?
This may help you :
$dom = new DOMDocument("1.0");
$parent = $dom->appendChild($dom->createElement("PARENT1"));
$parent->setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
$child = $root->appendChild($dom->createElement("CHILD1"));
$child->appendChild($dom->createTextNode('VALUE_FOR_CHILD1'));
$dom->encoding = "UTF-8";
$xml = $dom->saveXML();
echo $xml;
Will give you something like this :
<?xml version="1.0" encoding="UTF-8"?>
<PARENT1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CHILD1>VALUE_FOR_CHILD1</CHILD1>
</PARENT1>

Simple HTML Dom

Thanks for taking the time to read my post... I'm trying to extract some information from my website using Simple HTML Dom...
I have it reading from the HTML source ok, now I'm just trying to extract the information that I need. I have a feeling I'm going about this in the wrong way... Here's my script...
<?php
include_once('simple_html_dom.php');
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
//header("Content-Type: text/plain");
// create root element
$xmlProducts = $dom->createElement("products");
$dom->appendChild($xmlProducts);
$html = file_get_html('http://myshop.com/small_houses.html');
$html .= file_get_html('http://myshop.com/medium_houses.html');
$html .= file_get_html('http://myshop.com/large_houses.html');
//Define my variable for later
$product['image'] = '';
$product['title'] = '';
$product['description'] = '';
foreach($html->find('img') as $src){
if (strpos($src->src,"http://myshop.com") === false) {
$src->src = "http://myshop.com/$src->src";
}
$product['image'] = $src->src;
}
foreach($html->find('p[class*=imAlign_left]') as $description){
$product['description'] = $description->innertext;
}
foreach($html->find('span[class*=fc3]') as $title){
$product['title'] = $title->innertext;
}
echo $product['img'];
echo $product['description'];
echo $product['title'];
?>
I put echo's on the end for sake of testing...but I'm not getting anything... Any pointers would be a great HELP!
Thanks
Charles
file_get_html() returns a HTMLDom Object, and you cannot concatenate Objects, although HTMLDom have __toString methods when there concatenated there more then lilly corrupt in some way, try the following:
<?php
include_once('simple_html_dom.php');
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
//header("Content-Type: text/plain");
// create root element
$xmlProducts = $dom->createElement("products");
$dom->appendChild($xmlProducts);
$pages = array(
'http://myshop.com/small_houses.html',
'http://myshop.com/medium_houses.html',
'http://myshop.com/large_houses.html'
)
foreach($pages as $page)
{
$product = array();
$source = file_get_html($page);
foreach($source->find('img') as $src)
{
if (strpos($src->src,"http://myshop.com") === false)
{
$product['image'] = "http://myshop.com/$src->src";
}
}
foreach($source->find('p[class*=imAlign_left]') as $description)
{
$product['description'] = $description->innertext;
}
foreach($source->find('span[class*=fc3]') as $title)
{
$product['title'] = $title->innertext;
}
//debug perposes!
echo "Current Page: " . $page . "\n";
print_r($product);
echo "\n\n\n"; //Clear seperator
}
?>

Categories