xml generated not opening in browser - php

I wrote php code to create an xml but the xml file is not opening in browser
The following is the code:
$dom = new DOMDocument('1.0','UTF-8');
$dom->formatOutput = true;
$root = $dom->createElement('journal');
$dom->appendChild($root);
$journal_metadata = $dom->createElement('journal_metadata');
$dom->appendChild($journal_metadata);
$journal_metadata->appendChild($dom->createElement('full_title', 'Economics'));
$journal_metadata->appendChild($dom->createElement('abbrev_title', 'JJD'));
$issn = $dom->createElement('issn','2142');
$journal_metadata->appendChild($issn);
$issn->setAttribute('media_type', 'electronic');
$journal_metadata->appendChild($dom->createElement('doi_data', 'JJD'));
$doi_data = $dom->createElement('doi_data');
$journal_metadata->appendChild($doi_data);
$doi = $dom->createElement('doi', '10.E');
$doi_data->appendChild($doi);
$resource = $dom->createElement('resource', 'http://localhost/fo/journal.php?jid=1');
$doi_data->appendChild($resource);
// journal issue
$journal_issue = $dom->createElement('journal_issue');
$dom->appendChild($journal_issue);
// journal article
$journal_article = $dom->createElement('journal_article');
$dom->appendChild($journal_article);
$journal_article->setAttribute('publication_type', 'full_text');
$pages = $dom->createElement('pages');
$journal_article->appendChild($pages);
$resource = $dom->createElement('resource', htmlspecialchars('http://localhost/fo/abstracts.php?artID=472&jid=1'));
$doi_data->appendChild($resource);
echo '<xmp>'. $dom->saveXML() .'</xmp>';
$dom->save('result.xml') or die('XML Create Error');

Related

php, how to clone DOMElement?

Official documentation says that DOMElement has inherited method cloneNode http://php.net/manual/en/class.domelement.php . If i try to clone, it does not work. How to copy element from one DOMDocument to another? Namely, i have misplaced head, thus i have somehow to copy head and body, and than to echo them in right order.
ob_start();
$viewData = $this->data;
include_once( $this->viewTemplPath.$this->file );
$buffer = ob_get_clean();
$doc = new \DOMDocument();
$doc->loadHTML($buffer);
$head = $doc->getElementsByTagName('head')->item(0);
print_r('<br><br> 184 view.php head='); var_dump($head);
$body = $doc->getElementsByTagName('body')->item(0);
print_r('<br><br> 188 view.php body='); var_dump($body);
$docNew = new \DOMDocument();
$headNew = $head->cloneNone(true); // Fatal error: Call to undefined method DOMElement::cloneNone()
$docNew->appendChild($headNew);
$bodyNew = $body->cloneNone(true); // Fatal error: Call to undefined method DOMElement::cloneNone()
$docNew->appendChild($bodyNew);
echo $docNew->saveHTML();
In order to clone the Element, the solution is to import the Element i want to clone to the Document, and than add it as a child : http://php.net/manual/en/domdocument.importnode.php
This does not through errors, and echoes the new document.
But this does not resolve the problem with misplaced head.
ob_start();
$viewData = $this->data;
include_once( $this->viewTemplPath.$this->file );
$buffer = ob_get_clean();
$doc = new \DOMDocument();
$doc->loadHTML($buffer);
$head = $doc->getElementsByTagName('head')->item(0);
$body = $doc->getElementsByTagName('body')->item(0);
$docNew = new \DOMDocument();
$headNew = $docNew->importNode($head, true);
$docNew->appendChild($headNew);
$bodyNew = $docNew->importNode($body, true);
$docNew->appendChild($bodyNew);
echo $docNew->saveHTML();
You have typo mistake,
you wrote it CloneNone(true),
it should be cloneNode(true)

How to conect php with tally?

I want to connect php with tally and I want to store ledger in tally with php any idea?
I got reference from different site like :
1) http://www.tallysolutions.com/website/html/tallydeveloper/integration-capabilities.php
2)http://stackoverflow.com/questions/15717941/how-to-insert-data-into-tally-using-php
But it doesn't fulfill my requirement.
Thanks in advance
It's may help you
// create a new XML document
$doc = new DomDocument('1.0', 'UTF-8');
$envelope = $doc->appendChild($doc->createElement('ENVELOPE'));
//Header Section
$header = $envelope->appendChild($doc->createElement('HEADER'));
$version = $header->appendChild($doc->createElement('VERSION','6.3'));
$import = $header->appendChild($doc->createElement('TALLYREQUEST','Import'));
$type = $header->appendChild($doc->createElement('TYPE','Data'));
$id = $header->appendChild($doc->createElement('ID','All Masters'));
//End Header Section
//Body Section
$body = $envelope->appendChild($doc->createElement('BODY'));
$desc = $body->appendChild($doc->createElement('DESC'));
$static_var = $desc->appendChild($doc->createElement('STATICVARIABLES'));
$dup_combine = $static_var->appendChild($doc->createElement('IMPORTDUPS','##DUPCOMBINE'));
$data = $body->appendChild($doc->createElement('DATA'));
$tally_msg = $data->appendChild($doc->createElement('TALLYMESSAGE'));
//Ledger Data
foreach($contacts_data as $key => $value){
$ledger = $tally_msg->appendChild($doc->createElement('LEDGER'));
$parent=$ledger->appendChild($doc->createElement('PARENT',($value['contacts_types_id']=='1')?'Sundry Debtors':'Sundry Creditors'));
$name=$ledger->appendChild($doc->createElement('NAME',trim(str_replace( "&"," AND ",$value['name']))));
$address=$ledger->appendChild($doc->createElement('ADDRESS',trim(str_replace( "&"," AND ",$value['address']))));
$state=$ledger->appendChild($doc->createElement('STATENAME',trim(str_replace( "&"," AND ",$value['state_name']))));
$pincode=$ledger->appendChild($doc->createElement('PINCODE',$value['pincode']));
/*$ledger_contact=$ledger->appendChild($doc->createElement('LEDGERCONTACT',trim(str_replace( "&"," AND ",$value['contact_person']))));
$phone=$ledger->appendChild($doc->createElement('LEDGERPHONE',$value['phone']));
$fax=$ledger->appendChild($doc->createElement('LEDGERFAX',$value['fax']));
$mobile_no=$ledger->appendChild($doc->createElement('MOBILENO',$value['mobile_no']));
$sales_tax_no=$ledger->appendChild($doc->createElement('SALESTAXNO','23456789'));
$pan_no=$ledger->appendChild($doc->createElement('PANNO','453456789'));*/
$o['contacts_id'][]=$value['id'];
}
//End Ledger and Body Section
//Write the XML nodes
$fp = fopen(public_path()."/xml/import_tally/ledger.xml","wb");
if(fwrite($fp,$doc->saveXML())){
$o['tally_rslt']= shell_exec("curl -X POST tally_server_ip:port --data #".public_path()."/xml/import_tally/ledger.xml");
$o['modified_result']=strpos(preg_replace('/[0-9]+/', '', $o['tally_rslt']),'exists');
}
fclose($fp);
return $o;

Error while loading xml file?

Am dynamically loading an xml file and sending request to the api but getting
Warning: DOMDocument::loadXML(): Empty string supplied as input in /home/spotrech/public_html/ but this error is very inconsistent sometime appear sometime don't! I really no idea how to solve this. below is code
$rechargeApiUrl = "http://allrechargeapi.com/apirecharge.ashx?uid=$uid&apikey=$apike&number=$mobileNo&opcode=$opId&amount=$amount&ukey=$uniId&format=xml";
$url = file_get_contents($rechargeApiUrl);
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML(preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $url));
$itemInfo = $xmlDoc->getElementsByTagName('Result'); //returns an object.
$itemCount = $itemInfo->length;
foreach ($itemInfo as $userInfo) {
//Assigning node values to its specified variables.
$ukey = strtolower($userInfo->getElementsByTagName('ukey')->item(0)->childNodes->item(0)->nodeValue);
$status = $userInfo->getElementsByTagName('status')->item(0)->childNodes->item(0)->nodeValue;
$resultCode = $userInfo->getElementsByTagName('resultcode')->item(0)->childNodes->item(0)->nodeValue;
}
$strStatus = strtolower(trim($status));
$strResultCode = trim($resultCode);
$strCode = trim($ukey);
any response will be appreciated.Thank you

Retrieving Contact photo with Zend Gdata with PHP

When querying for contacts I use the code below to retrieve all my contacts
$gdata = new Zend_Gdata($client);
$gdata->setMajorProtocolVersion(3);
$query = new Zend_Gdata_Query(
"http://www.google.com/m8/feeds/contacts/default/full");
$feed = $gdata->getFeed($query);
As I look through each entry of the $feed I can get access to the contactId and according to the Contacts API I should be able to retrieve the picture by doing a GET on the following URL:
http://www.google.com/m8/feeds/photos/media/default/contactId
So I use the same mechanism to retrieve contacts and try to get a photo after setting $id:
$query = new Zend_Gdata_Query(
"http://www.google.com/m8/feeds/photos/media/default/$id");
$entryFeed = $gdata->getFeed($query);
But I get an error "DOMDocument cannot parse XML". Am I doing something wrong? Are there any example docs?
To get the photo use DOMXpath and search for the "//atom:link" tag, then use $gdata->get(href) to grab the photo. Check for the etag attribute for each link, this tells you whether or not there is a profile photo associated with this contact.
$doc = new DOMDocument;
$doc->recover = true;
$doc->loadXML($entry->getXML());
$xpath = new DOMXPath($doc);
$links = $xpath->query('//atom:link');
foreach($links as $link) {
if($link->getAttribute('etag') != "") {
$http_response = $gdata->get($link->getAttribute('href'));
$rawImage = $http_response->getBody();
$fp = fopen("/var/www/profile/$id.jpg", "w");
fwrite($fp, $rawImage);
fclose($fp);
break;
}
}

xml generation from php if file doesnt exist

This code works if file is previously expisting but if file doesn't exist this code doesnt work.
$doc = new DOMDocument();
$doc->version = '1.0';
$doc->encoding = 'ISO-8859-1';
$response = $doc->createElement('response');
$doc->appendChild($response);
$response_type= $doc->createElement('response_type','Yes');
$response_id = $doc->createElement('response_id',$max_id_site);
$response->appendChild($response_type);
$response->appendChild($response_id);
$doc->formatOutput = true;
echo $doc->saveXML();
$doc->save('$filename_xml');
updated code
$doc = new DOMDocument();
$doc->version = '1.0';
$doc->encoding = 'ISO-8859-1';
$response = $doc->createElement('response');
$doc->appendChild($response);
$response_type= $doc->createElement('response_type','Yes');
$response_id = $doc->createElement('response_id',$max_id_site);
$response->appendChild($response_type);
$response->appendChild($response_id);
$doc->formatOutput = true;
echo $doc->saveXML();
if (! is_file($filename_xml)) {
touch($filename_xml) or trigger_error("Can't Create File");
$doc->save($filename_xml);
}
You can replace
$doc->save('$filename_xml');
with
if (! is_file($filename_xml)) {
touch($filename_xml) or trigger_error("Can't Create File");
$doc->save($filename_xml);
}
Use file_exists() to check if file is already there.
Replace last line:
$doc->save('$filename_xml');
with
if( file_exists( $filename_xml ) == false ) {
$doc->save( $filename_xml );
}
In general you should not even generate the xml if file is there.
BTW: Putting $filename_xml) in "`" is wrong.

Categories