Announcement

Collapse
No announcement yet.

How to make a HttpRequest POST in SC ??

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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>u ser</UserName><Password>pass</Password><HotelId>id</HotelId></request>", HttpRequest::METH_POST);

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

    Thanks
    Nac
    Last edited by nacyil; 04-22-2015, 05:33 AM.

  • #2
    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
    Last edited by hoihoi661; 04-24-2015, 04:20 AM.

    Comment


    • #3
      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

      Comment


      • #4
        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);
        Albert Drent
        aducom software netherlands
        scriptcase partner, reseller, support and (turn-key) development
        www.scriptcase.eu / www.scriptcase.nl

        Comment


        • #5
          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=<request><UserName>USER</UserName><Password>PASS</Password><HotelId>ID</HotelId></request>');
          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 -> ' . curl_errno($ch) . ': ' . curl_error($ch);
          } else {
          $returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
          switch($returnCode){
          case 404:
          $result = 'ERROR -> 404 Not Found';
          break;
          default:
          break;
          }
          }

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

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

          $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>\n");

          }

          foreach($value->attributes() as $a => $b) {
          print($parent . "." . (string)$key . "." . (string)$a. "= ". (string)$b . "<BR>\n");
          }

          }
          return $child_count;
          ------------------------------------------------------------------------------------------------------------

          Comment

          Working...
          X