Archive for August, 2009

How to display XML output in Browser?

Posted by om 28 August, 2009 (1) Comment

To display XML file on browser use this header.

 
<?php
header("Content-type: text/xml");
 
?>
  • Share/Bookmark
Categories : PHP-XML Tags :

How to post XML using socket ?

Posted by om 28 August, 2009 (0) Comment

I had discussed posting XML over HTTP using CURL in last post. Remember that was first method.
As I had promised on earlier post I would like to share second method with you. That is socket!!
Use this code and send your XML file.

<?php
function postXMLToURL ($server, $path, $xmlDocument) {
$contentLength = strlen($xmlDocument);
$fp = fsockopen($server, 80, $errno, $errstr, 30);
fputs($fp, "POST $path HTTP/1.0rn");
fputs($fp, "Host: $serverrn");
fputs($fp, "Content-Type: text/xmlrn");
fputs($fp, "Content-Length: $contentLengthrn");
fputs($fp, "Connection: closern");
fputs($fp, "rn"); // all headers sent
fputs($fp, $xmlDocument);
$result = '';
while (!feof($fp)) {
$result .= fgets($fp, 128);
}
return $result;
}
 
function getBody ($httpResponse) {
$lines = preg_split('/(rn|r|n)/', $httpResponse);
$responseBody = '';
$lineCount = count($lines);
for ($i = 0; $i < $lineCount; $i++) {
if ($lines[$i] == '') {
break;
}
}
for ($j = $i + 1; $j < $lineCount; $j++) {
$responseBody .= $lines[$j] . "n";
}
return $responseBody;
}
 
$xmlpacket ='<AATHtlDispReq1>
<Agency>
<Iata>1234567890</Iata>
<Agent>lgsoftwares</Agent>
<Password>myapassword</Password>
<Brand>phpmind.com</Brand>
</Agency>
<Passengers>
<Adult AGE="" ID="1"></Adult>
<Adult AGE="" ID="2"></Adult>
</Passengers>
<DestCode>OGG</DestCode>
<CheckInDate>101009</CheckInDate>
</AATHtlDispReq1>';
 
 
$result = postXMLtoURL("www.yourdomain.com", "/path/",$xmlpacket);
 
$responseBody = getBody($result);
 
echo $responseBody;
?>

  • Share/Bookmark
Categories : PHP,PHP-XML Tags :

How to post XML using CURL?

Posted by om 28 August, 2009 (17) Comment

Recently I was working in a hotel booking engine and found a couple of methods to post XML to server; I thought this might be good to share with my friends who want to post xml via HTTP POST method.

There are several ways to Send XML requests via HTTP POST.
I am going to show you two ways. Both are very simple and easy.

As first approach I have used a small xml file with CURL.

 
<?php 
$xml_data ='<AATAvailReq1>'.
    '<Agency>'.
        '<Iata>1234567890</Iata>'.
        '<Agent>lgsoftwares</Agent>'.
        '<Password>mypassword</Password>'.
        '<Brand>phpmind.com</Brand>'.
    '</Agency>'.
    '<Passengers>'.
        '<Adult AGE="" ID="1"></Adult>'.
        '<Adult AGE="" ID="2"></Adult>'.
    '</Passengers>'.
'<HotelAvailReq1>'.
'<DestCode>JHM</DestCode>'.
        '<HotelCode>OGGSHE</HotelCode>'.
        '<CheckInDate>101009</CheckInDate>'.
        '<CheckOutDate>101509</CheckOutDate>'.
        '<UseField>1</UseField>'.
  '</HotelAvailReq1>'.  
  '</AATAvailReq1>';
 
 
$URL = "https://www.yourwebserver.com/path/";
 
			$ch = curl_init($URL);
			curl_setopt($ch, CURLOPT_MUTE, 1);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
			curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$output = curl_exec($ch);
			curl_close($ch);
 
?>

  • Share/Bookmark
Categories : PHP,PHP-XML Tags :

New PHP e-books

Posted by om 21 August, 2009 (0) Comment

If you are looking for any other specific book of php and mysql contact me!

  1. Beginning PHP4
  2. Integrating PHP and XML
  3. Object Oriented Programming with PHP5
  4. PHP5 MySQL Programming for the Absolute Beginner
  5. Php Mysql Programming For The Absolute Beginner
  6. PHP MySQL Programming
  • Share/Bookmark
Categories : Free PHP Books,PHP Tags :

How to delete a folder with PHP?

Posted by om 9 August, 2009 (0) Comment

To delete a file, or a folder and its contents i have compiled a recursive algorithm.
Hope this will be useful for all of you.

<?php
function rmdirr($dirname)
{
 
      // Sanity check
 
      if (!file_exists($dirname)) {
 
      return false;
 
      }
 
 
 
      // Simple delete for a file
 
      if (is_file($dirname) || is_link($dirname)) {
 
      return unlink($dirname);
 
      }       
 
      // Loop through the folder
 
      $dir = dir($dirname);
 
      while (false !== $entry = $dir->read()) {
 
      // Skip pointers
 
      if ($entry == '.' || $entry == '..') {
 
      continue;
 
      }
 
      // Recurse
      rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
 
      }
 
      // Clean up
      $dir->close();
 
      return rmdir($dirname);
 
      }
 
?>
  • Share/Bookmark
Categories : AJAX Tags :

How to Stop SQL Injection in MYSQL?

Posted by om 9 August, 2009 (2) Comment

Every PHP-MYSQL programmer need to know Anti-SQL Injection.

Please take a look at very simple function which can save your database!!

<?Php
 
function ClearInput($dirty){
 
	if (get_magic_quotes_gpc()) {
 
	$clean = mysql_real_escape_string(stripslashes($dirty));
 
	}else{
 
	$clean = mysql_real_escape_string($dirty);
 
	}
	return $clean;
 
}
 
?>
  • Share/Bookmark
Categories : Easy PHP,MySQL,PHP Tags :