SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: php n xml

  1. #1
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php n xml

    i have this site that i just did, that uses xml to pull info from my supplier.

    i'm using this.

    Code:
    function send_request_test($data, $path, $host) {    
        $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 = 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;
    }
    
    $test_xml = "
    <?xml version ='1.0' encoding='UTF-8'?>
    		<root>
    			<header>
    				<protocol>1</protocol>
    				<oem>supplier</oem>
    				<agentID>11111</agentID>
    				<password>password</password>	
    				<sessionID>5bcsx0ut3e0pck45fikpgd55</sessionID>
    			</header>
    			<body>
    				<opCode>OP_SET_INFO</opCode>
    				<deferPayment>0</deferPayment>
    				<reserveDetails>
    					<step>SAVE_RES</step>
    				</reserveDetails>
    				<pfileNumOnlyInResponse></pfileNumOnlyInResponse>
    			</body>
    			</root>
    ";
    
    $test_content = send_request_test($test_xml, $test_path, $test_host);
    
    $pos = strpos($test_content, '<body>');
    
    if ($pos === false) {
        //echo "The string '$findme' was not found in the string '$mystring'";
    } else {
    	$test_data = substr($test_content, $pos);
    	$xml_parser = xml_parser_create();
    	xml_set_element_handler($xml_parser, "startTag_reservation", "endTag_reservation");
    	xml_set_character_data_handler($xml_parser, "contents_test_reservation");
    
    	if(!(xml_parse($xml_parser, $test_data))){
    		die("Error on line " . xml_get_current_line_number($xml_parser));
    	}
    
    	xml_parser_free($xml_parser);
    }
    the above code appears to send and receive the data i requested. However, the sessionID that is supposed to be returned with the data changes with each response. This should not be the case, as i tried sending the exact same xml via a standalone xml program (SendXML) the sessionID remained consistent.

    Is there anything that i might have missed or included that somehow resets the xml request? or anything? i was stuck on this for 1 whole week before it suddenly hit me that the sessionid wasn't consistent.

    Please help!

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there anything that i might have missed or included that somehow resets the xml request?
    My guess would be the fsockopen().
    It probably ignores the session cookie, thus the target server keeps sending a new one.
    If this is important to you, try to use curl functions to fetch the datas.
    You can specify that it must keep the cookies from query to query, by using a "query jar" to store them
    http://www.php.net/manual/en/function.curl-setopt.php
    CURLOPT_COOKIEFILE The name of the file containing the cookie data. The cookie file can be in Netscape format, or just plain HTTP-style headers dumped into a file.
    CURLOPT_COOKIEJAR The name of a file to save all internal cookies to when the connection closes.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •