PHP CURL POST Request trouble

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shanecody
Forum Newbie
Posts: 23
Joined: Fri May 08, 2009 4:12 pm

PHP CURL POST Request trouble

Post by shanecody »

I am getting the following error while running the my attached php script:
HTTP/1.1 100 Continue

HTTP/1.1 500 Internal Server Error
Connection: close
Server: Jetty(6.1.12.rc2)

I know that the simplexml_load_string() causes errors right now. Going to make it more robust when I get the request working.

Thanks in advance!

the script:

Code: Select all

 
<?php
 
// sends a CURL request to REX, defaults to GET, returns simple_xml array 
function send_request($url, $link_array = NULL, $method = "GET")
{   
        // make sure url is safe
        rawurlencode($url);
        
        $headers = array('POST /rexwapi/avail/products HTTP/1.1',
                                         'content-type: application/vnd.enservia.dto+xml',
                                         'Accept: application/vnd.enservia.summary+xml',
                                         'Cache-Control: no-cache',
                                         'Pragma: no-cache',
                                         'User-Agent: Java/1.6.0_10-beta',
                                         'Host: 76.76.241.158:10090',
                                         'Connection: keep-alive',
                                         'Authorization: Basic cmV4LXJleDpyZXgtNC1yZXg=',
                                         'Content-Length: 1122');
        
        $postfeild = "";
        
        // if GET needs params checks for get value, then if $link_array isn't NULL encodes the  URL 
        if($method == "GET")
        {
            if($link_array){
                $first = 0;
                $url .= "?";        
                foreach($link_array as $key => $value){
                    if($first == 0){
                        $url .= $key . "=" . urlencode($value);
                        $first = 1;
                    } else {
                        $url .= "&" . $key . "=" . urlencode($value);
                    }
                }
            }
        } else if($link_array){
                $post = $link_array;
                //echo "<pre>{$post}</pre><hr />";
        }
        
        // make sure url is safe 
        $url = htmlspecialchars($url);
 
        // echoes $url for error testing
        echo "<b><a href=\"{$url}\">{$url}</a></b><hr />";
        
        // curl setup and initialise 
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_PORT, 10090);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, "rex-rex:rex-4-rex");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        if($method == "POST"){
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
        }
        // grab URL and pass it to the browser with string containing XML
        $output = curl_exec($ch);
        var_dump($output);
        echo "<hr />";
        
        // places CURL response into an array
        $result = simplexml_load_string($output);
    
        //curl_exec($ch);
        echo curl_error($ch);
        // close cURL resource, and free up system resources
        curl_close($ch);
        
        // return curl array 
        return $result;
}
 
$urlPost = "http://76.76.241.158:10090/rexwapi/avail/products";
 
$linkarray = "
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
   <request type=\"entity\">
      <merchandiseCategory type=\"token\"/>
      <merchandiseType type=\"token\" key=\"1270\">Jeep Tour</merchandiseType>
      <facility type=\"token\"/>
      <productName type=\"text\"/>
      <provider type=\"token\"/>
      <style type=\"token\"/>
      <country type=\"text\"/>
      <city type=\"text\"/>
      <routeNumber type=\"text\"/>
      <endFacility type=\"token\"/>
      <daysWithinTarget type=\"integer\">0</daysWithinTarget>
      <minDuration type=\"integer\">1</minDuration>
      <maxDuration type=\"integer\">1</maxDuration>
      <shopper type=\"token\"/>
      <currency type=\"text\"/>
      <unitsNeeded type=\"integer\">0</unitsNeeded>
      <crs type=\"text\"/>
      <market type=\"token\"/>
      <product type=\"token\"/>
      <candidate type=\"token\"/>
      <startDate type=\"date\"></startDate>
      <endDate type=\"date\"></endDate>
      <duration type=\"text\"/>
      <destination type=\"token\"/>
      <endTime type=\"text\">09:00</endTime>
      <startTime type=\"text\">09:00</startTime>
      <travellers type=\"list\">
         <traveller type=\"entity\">
            <name type=\"text\">Adult1</name>
            <age type=\"token\" key=\"-1\">Adult</age>
         </traveller>
         <traveller type=\"entity\">
            <name type=\"text\">Adult2</name>
            <age type=\"token\" key=\"-1\">Adult</age>
         </traveller>
      </travellers>
   </request>";                    
 
$postresponce = send_request($urlPost, $linkarray, "POST");
echo "<hr />";
print_r($postresponce);
 
 
    echo "<hr />";
echo "<pre>";
foreach ($postresponce->product as $key => $value){
    //print_r($value->@attributes('key'));
        print_r($value);
        $i++;
} 
echo "</pre>";
?>
Last edited by Benjamin on Tue Jun 09, 2009 11:20 pm, edited 1 time in total.
Reason: Added [code=php] tags.
Post Reply