note 51360 modified in ref.curl by danbrown

From: Date: Sun, 17 May 2009 19:39:49 +0000
Subject: note 51360 modified in ref.curl by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155250@lists.php.net to get a copy of this message
Had one heck of a time getting curl to send my XML request. Tried a lot of different things, FINALLY ended up with this. Im making a curl request to paymentech / Orbital 's payment gateway. Essentially, I took the same header i made for the fsockopen() request, and used it for this curl request. (Hostinc company didnt allow fsockopen(). You'll note that this specific gateway requires a custom content type header. Thats what gave me the most trouble. When using the CURLOPT_HTTPHEADER, and CURLOPT_POSTFIELDS together, it doesnt matter, it sends the default post header because of CURLOPT_POSTFIELDS. <?php $url = "https://example.com"; $page = "/proccessing_page.cgi"; $post_string = "<YourXML>All XML stuff Here</YourXML>"; $header = "POST ".$page." HTTP/1.0 \r\n"; $header .= "MIME-Version: 1.0 \r\n"; $header .= "Content-type: application/PTI26 \r\n"; $header .= "Content-length: ".strlen($post_string)." \r\n"; $header .= "Content-transfer-encoding: text \r\n"; $header .= "Request-number: 1 \r\n"; $header .= "Document-type: Request \r\n"; $header .= "Interface-Version: Test 1.4 \r\n"; $header .= "Connection: close \r\n\r\n"; $header .= $post_string; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 4); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header); $data = curl_exec($ch); if (curl_errno($ch)) { print curl_error($ch); } else { curl_close($ch); } // use XML Parser on $data, and your set! $xml_parser = xml_parser_create(); xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0); xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE,1); xml_parse_into_struct($xml_parser, $data, $vals, $index); xml_parser_free($xml_parser); // $vals = array of XML tags. Go get em! ?> Worked like a dream for me. Enjoy! --was-- Had one heck of a time getting curl to send my XML request. Tried a lot of different things, FINALLY ended up with this. Im making a curl request to paymentech / Orbital 's payment gateway. Essentially, I took the same header i made for the fsockopen() request, and used it for this curl request. (Hostinc company didnt allow fsockopen(). You'll note that this specific gateway requires a custom content type header. Thats what gave me the most trouble. When using the CURLOPT_HTTPHEADER, and CURLOPT_POSTFIELDS together, it doesnt matter, it sends the default post header because of CURLOPT_POSTFIELDS. <?php $url = "https://your.gateway.url"; $page = "/proccessing_page.cgi"; $post_string = "<YourXML>All XML stuff Here</YourXML>"; $header = "POST ".$page." HTTP/1.0 \r\n"; $header .= "MIME-Version: 1.0 \r\n"; $header .= "Content-type: application/PTI26 \r\n"; $header .= "Content-length: ".strlen($post_string)." \r\n"; $header .= "Content-transfer-encoding: text \r\n"; $header .= "Request-number: 1 \r\n"; $header .= "Document-type: Request \r\n"; $header .= "Interface-Version: Test 1.4 \r\n"; $header .= "Connection: close \r\n\r\n"; $header .= $post_string; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 4); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header); $data = curl_exec($ch); if (curl_errno($ch)) { print curl_error($ch); } else { curl_close($ch); } // use XML Parser on $data, and your set! $xml_parser = xml_parser_create(); xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0); xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE,1); xml_parse_into_struct($xml_parser, $data, $vals, $index); xml_parser_free($xml_parser); // $vals = array of XML tags. Go get em! ?> Worked like a dream for me. Enjoy! http://php.net/manual/en/ref.curl.php

« previous php.notes (#155250) next »