PHP Classes

File: client.php

Recommend this page to a friend!
  Classes of tuhin   API   client.php   Download  
File: client.php
Role: Example script
Content type: text/plain
Description: This is client site php module
Class: API
Receive and reply to Web services requests
Author: By
Last change:
Date: 15 years ago
Size: 3,640 bytes
 

Contents

Class file image Download
<?php
/*
 * Created on 2008/12/03
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

if($HTTP_RAW_POST_DATA){
$xml = trim($HTTP_RAW_POST_DATA); // get rid of excess white space that may or may not be present
//print_r($xml);
 /* Set _tag variables to the position in the file where it can be found
  the strpos() function returns false on failure to find the given string
  Since this is our API, we know what to look for and what is needed
 */
 
$myXML_tag = strpos($xml, "<myXML>"); // 7 - this is the string length
 
$function_tag = strpos($xml, "<function>"); // 10
 
$orderID_tag = strpos($xml, "<orderID>"); // 9
 
$reference_tag = strpos($xml, "<reference>"); // 11

 
 
$j = $function_tag + 10;
 
// grab the first char after the open tag
 
$function = substr($xml, $j, 1);
 
 
/*
   grab the characters until you hit a '<'. Note in your manual that you write with your API that '<' characters will cause a fault. It is your API so, you can do whatever you want! Technically, you're only bound by the rules of the XML language.
  */
 
for ($i = $j + 1; substr($xml, $i, 1) != "<"; $i++) {
  
$function .= substr($xml, $i, 1);
  }

 
$j = $orderID_tag + 9;
 
$orderID = substr($xml, $j, 1);
 
  for (
$i = $j + 1; substr($xml, $i, 1) != "<"; $i++) {
  
$orderID .= substr($xml, $i, 1);
  }
 
 
$j = $reference_tag + 11;
 
$reference = substr($xml, $j, 1);
  for (
$i = $j + 1; substr($xml, $i, 1) != "<"; $i++) {
  
$reference .= substr($xml, $i, 1);
  }

 
 
 
$xml = new DOMDocument('1.0');
   
//$xml = domxml_new_doc("1.0");

  // create the elements
 
$root = $xml->createElement("myXML");
 
$values = $xml->createElement("values");
 
$id = $xml->createElement("orderID");
 
$ref = $xml->createElement("reference");
 
$response = $xml->createElement("response");
 
$order = $xml->createElement("order");

 
// create the text nodes
 
$id_txt = $xml->createTextNode($orderID);
 
$ref_txt = $xml->createTextNode($reference);
 
$ord_txt = $xml->createTextNode("confirmed");
 
  
$root = $xml->appendChild($root);
 
$values = $root->appendChild($values);
 
$id = $values->appendChild($id);
 
$id_txt = $id->appendChild($id_txt);
 
$ref = $values->appendChild($ref);
 
$ref_txt = $ref->appendChild($ref_txt);
 
$response = $root->appendChild($response);
 
$order = $response->appendChild($order);
 
$ord_id = $order->appendChild($ord_txt);
 
 
$xml = $xml->saveXML();

   
   
 
$File = "THIS XML PAHT /api/built.xml";
   
$fp = fopen($File, "wr");
     
fwrite($fp, $xml);
     
fclose($fp);
     
print_r($orderID);
print_r("<br>");
print_r($reference);
print_r("<br>");
header("Location : http:// YOUR SERVER NAME /api/builtt.xml");
require_once(
"result.php");
exit;
}else{
 
 
$request = '<?xml version="1.0"?>
<myXML>
<function>order</function>
<values>
<orderID>RR123</orderID>
<reference>235646</reference>
</values>
</myXML>
'
;

$server = "CONNECT SERVER NAME ";
$url = 'http://CONNECT SERVER NAME /api/my_server.php'; // THIS URL FROM WHICH YOU WANT SEND AND GET INFORMATION

$content_length = strlen($request);
$headers= "POST $url HTTP/1.0\r\nContent-type: text/xml\r\nHost: $server\r\nContent-length: $content_length\r\n\r\n";
$fp = fsockopen($server, 80, $errno, $errstr, 30);
if (!
$fp) return false;
fputs($fp, $headers);
fputs($fp, $request);
$ret = "";
while (!
feof($fp)) {
$ret.= fgets($fp, 1024);
}
fclose($fp);
$datas = split("\r\n\r\n", $ret, 2);
$header = $datas[0];
$body = $datas[1];
echo
$body;
exit;

}


?>