How to make a HttpRequest POST in SC ??

Hello,

I would like to use httprequest POST in my project, my try this but it doesn’t recognize httprequest command !!!
I think I need to add a librairy or extensions but which one ? ?

$r = new HttpRequest(“https://xxxxxxxxxxx.com/services/service.asmx/RequestReservations?XMLstring=<request><UserName>user</UserName><Password>pass</Password><HotelId>id</HotelId></request>”, HttpRequest::METH_POST);

$result = $r->send();
echo “result :”.$result;

Thanks
Nac

What you’re trying to do is a GET, a POST is where you don’t see the variables in the URL. Which might be causing some problems.
Far as I know this is just integrated into PHP and no special libraries should be needed.

EDIT: Btw, if you’re actually trying to do a GET, the same code should work just replace METH_POST with METH_GET

http://php.net/manual/en/function.http-post-fields.php
Example: http://syframework.alwaysdata.net/http-post-fields

Google is your friendly neigbourhood search engine

In addition: one other option to ‘call’ an url is the following (despite the post/get issue).

$url='https://www.mysite.com/start?rtlo='.$rtlo.'&reporturl='.$repurl; // etc.
$arr = file_get_contents($url);

Thanks for answering …

After some search and test, this is a working code using cURL for XML POST to a webservice :
As somebody needed

Nac


define('XML_PAYLOAD', 'xmlstring=&lt;request&gt;&lt;UserName&gt;USER&lt;/UserName&gt;&lt;Password&gt;PASS&lt;/Password&gt;&lt;HotelId&gt;ID&lt;/HotelId&gt;&lt;/request&gt;');
define('XML_POST_URL', 'https://...url..../RequestReservations');
	
/**
 * Initialize handle and set options
 */
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, XML_POST_URL); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 4); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('content-type text/xml'));

$start = array_sum(explode(' ', microtime()));
$result = curl_exec($ch); 
$stop = array_sum(explode(' ', microtime()));
$totalTime = $stop - $start;
/**
 * Check for errors
 */
if ( curl_errno($ch) ) {
	$result = 'ERROR -&gt; ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
	$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
	switch($returnCode){
		case 404:
			$result = 'ERROR -&gt; 404 Not Found';
			break;
		default:
			break;
	}
}

/**
 * Close the handle
 */
curl_close($ch);

echo 'Total time for request: ’ . $totalTime . "
";

$xml = new SimpleXMLElement($result);
RecurseXML($xml,"");


function RecurseXML($xml, $parent)

$child_count = 0;
foreach($xml as $key=>$value)
{
$child_count++;
if(RecurseXML($value,$parent.".".$key) == 0) // no childern
{
print($parent . “.” . (string)$key . " = " . (string)$value . "<BR>
");

  }
    
  foreach($value-&gt;attributes() as $a =&gt; $b) { 
     print($parent . "." . (string)$key . "." . (string)$a. "= ". (string)$b . "&lt;BR&gt;

");
}

}
return $child_count;