php n xml problem

Discussion in 'PHP' started by gingar, Dec 18, 2008.

  1. #1
    At present, i'm using curl. I am able to send the xml query over, and get a response. but the response is not what i am expecting.

    This is the code im using..

    $url = "http://xxx";
    $xml = '<?xml version ="1.0" encoding="UTF-8"?>
             <root>
                <header>
                   <protocol>1</protocol>
                   <oem>xx</oem>
                   <agentID>xxx</agentID>
                   <password>xxxx</password>
                </header>
                <body>
                   <opCode>XX_AREAS_XX</opCode>
                   <item name=”ANTALYA” ID=”5” />
                   <item name=”JERUSALEM” ID=”1” />
                </body>
    
             </root>
          ';
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'xml=' . $xml);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($ch); 
    
    PHP:

    when i use a standalone program to submit the exact same xml string to the same url, using post, i get this reply.

    <body><version>3.91</version><opCode>OP_AREAS_LIST</opCode><country ID="27"><item name="Aachen, Germany" ID="13547" coreID="0"/><item name="Augsburg, Germany" ID="4408" coreID="0"/><item name="Bad Homburg, Germany" ID="5867" coreID="0"/><item name="Baden-Baden, Germany" ID="229" coreID="0"/><item name="Bautzen, Germany" ID="5889" coreID="0"/></country></body>
    
    Code (markup):

    but when i use the above code, i get this instead..

    <body><version>3.91</version><opCode>OP_OK</opCode></body>
    
    Code (markup):

    is there something wrong with the way i'm executing the code?
     
    gingar, Dec 18, 2008 IP
  2. brownskinman

    brownskinman Peon

    Messages:
    18
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Kindly give this a shot:

    <?php
    function send_request($data) {
    	# url = http://www.example.com/path/to/receiver
    	$path = '/path/to/receiver';
    	$host = 'www.example.com';
    	
    	$header = "POST ".$path." HTTP/1.0\r\n";
    	$header .= "Host: ".$host."\r\n";
    	$header .= "Content-Type: text/xml\r\n";
    	$header .= "Content-Length: " . strlen($data) . "\r\n\r\n";
    	// open socket
    	# $fp = pfsockopen('ssl://www.example.com', 443, $errno, $errstr, 60);
    	$fp = fsockopen($host, 80, $errno, $errstr, 60);
    	$retval='';
    	if (!$fp) {
    		return "Error: " . $errstr;
    	}
    	else {
    		fputs($fp, $header.$data);
    		while (!feof($fp)) {
    			$retval .= fread($fp, 1024);
    		}
    		fclose ($fp);
    	}
    	return $retval;
    }
    $xml = '<xml>xml</xml>';
    echo send_request($xml);
    ?>
    PHP:
     
    brownskinman, Dec 18, 2008 IP
  3. brownskinman

    brownskinman Peon

    Messages:
    18
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <?php
    	$path = '/path/to/receiver';
    	$host = 'example.com';
    	$content = '<xml>xml</xml>';
    	
    	// you may use parse_url here instead
    	$url = 'http://www.example.com/path/to/receiver';
    			
    	$headers = array();
    	$headers[] = "POST ".$path." HTTP/1.1";
    	$headers[] = "Host: ".$host;
    	$headers[] = "Content-Type: text/xml";
    	$headers[] = "Content-Length: ".mb_strlen($content, "UTF-8");
    	$headers[] = "Connection: Close";
    	// add the headers
    
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
    	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //add http headers
    	curl_setopt($ch, CURLOPT_HEADER, 1);
    	curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
    	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
    	curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    	curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $content);  // post the xml
    	
    	$errno = curl_errno($ch);
    	$response = curl_exec($ch);
    	
    	echo $response;
    ?>
    PHP:
     
    brownskinman, Dec 18, 2008 IP
  4. gingar

    gingar Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks brownskinman,

    i tried the curl example you posted, i still get the same 1 liner response with some additional info.

    
    HTTP/1.1 200 OK
    Connection: close
    Date: Fri, 19 Dec 2008 03:22:03 GMT
    Server: Microsoft-IIS/6.0
    X-Powered-By: ASP.NET
    Content-Length: 60
    Content-Type: text/xml
    Expires: Fri, 19 Dec 2008 03:21:03 GMT
    Cache-control: private
    
    <body><version>3.91</version><opCode>OP_OK</opCode></body>
    
    Code (markup):
    then i tried the fput method,
    IT WORKS!!!! however, it takes almost 15 seconds to return this result.. that's considered long right? or is it because of the data length that is being returned that's why its so slow?
     
    gingar, Dec 18, 2008 IP