Web Analytics Made Easy -
StatCounter accessing curl post data - CodingForum

Announcement

Collapse
No announcement yet.

accessing curl post data

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

  • accessing curl post data

    I'm working with an api which is going to send data to my php script but I don't know how to access it. The data will be xml and as far as I can tell there will be no key with the post fields. This is based on their php class (and also that they don't specify anything in the documentation) which they provided as an example of how to send data to them.

    This is an example of the code in their class:

    PHP Code:
    function cancelBooking($reservationID){
            
    $post[] = "<Push_CancelReservation_RQ>
                  <Authentication>
                    <UserName>"
    .$this->username."</UserName>
                    <Password>"
    .$this->password."</Password>
                  </Authentication>
                  <ReservationID>
    $reservationID</ReservationID>
               </Push_CancelReservation_RQ>"
    ;   
            
    $x $this->curlPushBack($this->server_url,$post);  
            return 
    simplexml_load_string($x['messages']);            
        }
        
        
    /**
        * Default Curl connection
        * 
        * @param mixed $url
        * @param mixed $post_fields
        * @param mixed $head
        * @param mixed $follow
        * @param mixed $header
        * @param mixed $referer
        * @param mixed $is_ssl
        * @param mixed $debug
        */         
                      
        
    function curlPushBack($url$post_fields ""$head 0$follow 1$header=""$referer=""$is_ssl false$debug 0){

            
    $ch curl_init ();

            
    $header[]="Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
            
    $header[]="Accept-Language: en-us";
            
    $header[]="Accept-Charset: SO-8859-1,utf-8;q=0.7,*;q=0.7";
            
    $header[]="Keep-Alive: 300";
            
    $header[]="Connection: keep-alive";

            
    curl_setopt ($chCURLOPT_HEADER$head);
            
    curl_setopt ($chCURLOPT_FOLLOWLOCATION$follow);
            
    curl_setopt ($chCURLOPT_RETURNTRANSFER1);
            
    curl_setopt ($chCURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.0; en; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4");
            
    curl_setopt ($chCURLOPT_HTTPHEADER, array
                  (
                      
    'Content-type: application/x-www-form-urlencoded; charset=utf-8',
                      
    'Set-Cookie: ASP.NET_SessionId='.uniqid().'; path: /; HttpOnly'
                  
    ));
            
    curl_setopt ($chCURLOPT_REFERER,$referer);
            
    curl_setopt ($chCURLOPT_URL,$url);
            
    curl_setopt($chCURLOPT_SSL_VERIFYPEER$is_ssl);
            
    curl_setopt($chCURLOPT_SSL_VERIFYHOST$is_ssl);
            
    curl_setopt($chCURLOPT_FRESH_CONNECTtrue);

            if (
    $post_fields != ""){
               if(
    is_array($post_fields)){
                  
    $post_fields implode("&",$post_fields);
               }
               
    curl_setopt ($chCURLOPT_POST,1);
               
    curl_setopt ($chCURLOPT_POSTFIELDS,$post_fields);
            }

            
    $result=curl_exec($ch);
            
    $err=curl_error($ch);

            
    $results["messages"] = $result;
            
    $results["errors"] = $err;
            
    curl_close($ch);
            return 
    $results;
        } 
    I've tried using the same method to test a post of data to my own script in the same way but I can't find the data, its not in $_POST, $_GET, $_REQUEST or $_SERVER as far as I can tell. This is all the documentation says:

    The system can call your system to transfer information about a new reservation. To use this feature you should create a HTTP/HTTPS request handler. The
    system will post this service (with a specific XML content) every time someone puts a
    confirmed reservation/lead for your property or cancels it. RLNM simply posts your handler and
    does not expect a response.
    http://www.hazelryan.co.uk

  • #2
    Are you trying to implement this method for yourself, where you can make any changes that would simplify and clarify what the code and data is or are you just testing locally and are going to have to submit data to someone else's api, where the data format must be what is shown in the code?

    Despite the code using CURLOPT_POST and CURLOPT_POSTFIELDS, it's submitting the raw xml to the url. You would need to access the raw post data, and parse it yourself.

    If you are doing this for yourself, just produce an array of key/value pairs, rather than xml, and receive the expected $_POST data on the receiving end.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

    Comment


    • #3
      If you must use this data format, see the following 'handler' to read and do something with the data -

      PHP Code:
      <?php
      if($_SERVER['REQUEST_METHOD'] == 'POST')
      {
          
      $data file_get_contents("php://input");
          
      // parse the xml in $data here...
          
          // log the data to see what it is
          
      file_put_contents('log.txt',$data "\n",FILE_APPEND);
      }
      Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

      Comment

      Working...
      X