thedethfox
thedethfox

Reputation: 1741

PHP's curl not posting data to the server

I have written a small app that uses curl a while ago and was working. Today I tried to run it and it is not working.

I use curl to post data to another page located on an external server. The Get is working but not the post.

Update 1:

I have narrowed the error down to having an entry called "SOME_KEY" and having the value "'@C:/wamp/www/name.pdf'" in the posts array will cause this problom.

Howcome the @ symbole is causing a problem!!! I am trying to uplaod a file to the server

Solved

Check the answer below.

Original entry

My current curl options

    $curl_options = array(
        CURLOPT_RETURNTRANSFER => true,                 // Return web page      
        CURLOPT_HEADER         => false,                // Don't return headers
        CURLOPT_FOLLOWLOCATION => false,                // Follow redirects
        CURLOPT_ENCODING       => "",                   // Handle all encodings
        CURLOPT_AUTOREFERER    => false,                // Set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,                  // Timeout on connect
        CURLOPT_TIMEOUT        => 120,                  // Timeout on response
        CURLOPT_MAXREDIRS      => 0,                    // Stop after 10 redirects
        CURLOPT_USERAGENT      => "regify connector",   // Who am I? FIREFOX, IE, OPERA, CHROME, SPIDER, SEARCH BOT...etc.
    );

the function that posts the data:

function makePostRequest($url, $fields  ){

        //Create the options
        //global $curl_options;
        $curl_options[CURLOPT_URL       ] = $url;
        $curl_options[CURLOPT_POST      ] = count($fields);
        $curl_options[CURLOPT_POSTFIELDS] = $fields;

        $ch = curl_init();
        curl_setopt_array($ch, $curl_options);

        $content = curl_exec ($ch);
        $err     = curl_errno($ch);

        curl_close($ch);    
        return ($err == 0)?$content:false;
    }

PHP's getallheader is returning

array
  'Host' => string 'localhost' (length=9)
  'Accept' => string '*/*' (length=3)
  'Content-Length' => string '16632433' (length=8)
  'Expect' => string '100-continue' (length=12)
  'Content-Type' => string 'multipart/form-data; boundary=----------------------------93ac4fed717b' (length=70)

CURL's curl_getinfo function is returning:

    array
  'url' => string 'http://localhost/archieve/index.php' (length=35)
  'content_type' => string 'application/json' (length=16)
  'http_code' => int 200
  'header_size' => int 281
  'request_size' => int 263
  'filetime' => int -1
  'ssl_verify_result' => int 0
  'redirect_count' => int 0
  'total_time' => float 0.125
  'namelookup_time' => float 0
  'connect_time' => float 0
  'pretransfer_time' => float 0
  'size_upload' => float 16632433
  'size_download' => float 18
  'speed_download' => float 144
  'speed_upload' => float 133059464
  'download_content_length' => float 18
  'upload_content_length' => float 16632433
  'starttransfer_time' => float 0.015
  'redirect_time' => float 0
  'certinfo' => 
    array
      empty

Can this porblem be cause by the content type? I read on stackoverflow that it recommaded that CURL is smart enough to set this value by itself.

Upvotes: 1

Views: 533

Answers (1)

thedethfox
thedethfox

Reputation: 1741

I figured it out. The code was and still working as it should. Curl was not able to post the PDF file, I believe because of permissions, and therefore it was not posting at all.

Upvotes: 1

Related Questions