posting with cURL problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zorro

    #1

    posting with cURL problem

    Hello there,

    I can't figure out why is it that when i use an array for my postfields
    it doesn't work :

    this works
    curl_setopt($cu rl, CURLOPT_POSTFIE LDS, "clown=bozo " );

    this doesn't
    curl_setopt($cu rl, CURLOPT_POSTFIE LDS, array('clown'=> 'bozo') );


    The web site i post to only responds when my parameters are in a
    string, but it sends nothing back when i use an array - no headers, no
    error, nothing. And cURL shows no error either. So there must be
    something different in the way cURL posts from an array. It's like the
    web site reads it differently than when i post a string and possibly
    some error is generated on the server. I make other posts to the same
    web site and all is okay except when i use an array. Any ideas what
    might be going on?

    Here is the full code

    $curl = curl_init();

    curl_setopt($cu rl, CURLOPT_COOKIEJ AR, 'cookiejar.txt' );
    curl_setopt($cu rl, CURLOPT_COOKIEF ILE, 'cookiejar.txt' );
    curl_setopt($cu rl, CURLOPT_USERAGE NT, 'Mozilla/5.0 (Windows; U; Windows
    NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6');
    curl_setopt($cu rl, CURLOPT_RETURNT RANSFER, 1);
    curl_setopt($cu rl, CURLOPT_HEADER, 1);
    curl_setopt($cu rl, CURLOPT_MAXREDI RS, 2);
    curl_setopt($cu rl, CURLOPT_FOLLOWL OCATION, 1);

    curl_setopt($cu rl, CURLOPT_POST, 1);
    curl_setopt($cu rl, CURLOPT_POSTFIE LDS, array('clown'=> 'bozo') );

    curl_setopt($cu rl, CURLOPT_URL, 'http://www.somesite.co m/form.htm');
    $response = curl_exec($curl );

    echo $response; // this echos nothing and when saved to a file the
    file is 0 bytes...

  • Benjamin

    #2
    Re: posting with cURL problem


    zorro wrote:
    Hello there,
    >
    I can't figure out why is it that when i use an array for my postfields
    it doesn't work :
    >
    this works
    curl_setopt($cu rl, CURLOPT_POSTFIE LDS, "clown=bozo " );
    >
    this doesn't
    curl_setopt($cu rl, CURLOPT_POSTFIE LDS, array('clown'=> 'bozo') );
    If you look at the php manual (http://www.php/curl_setop), you'll
    notice that the option CURLOPT_POSTFIE LDS only takes a string as it's
    parameter. PHP doesn't convert the array into the proper POST fields by
    itself. You have to make a function for this your self.
    Try this:
    function array_to_post($ array) {
    $post = "";
    foreach ($array as $name =$value) {
    $post .= "&$name=$value" ;
    }
    return $post;
    }
    >
    >
    The web site i post to only responds when my parameters are in a
    string, but it sends nothing back when i use an array - no headers, no
    error, nothing. And cURL shows no error either. So there must be
    something different in the way cURL posts from an array. It's like the
    web site reads it differently than when i post a string and possibly
    some error is generated on the server. I make other posts to the same
    web site and all is okay except when i use an array. Any ideas what
    might be going on?
    >
    Here is the full code
    >
    $curl = curl_init();
    >
    curl_setopt($cu rl, CURLOPT_COOKIEJ AR, 'cookiejar.txt' );
    curl_setopt($cu rl, CURLOPT_COOKIEF ILE, 'cookiejar.txt' );
    curl_setopt($cu rl, CURLOPT_USERAGE NT, 'Mozilla/5.0 (Windows; U; Windows
    NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6');
    curl_setopt($cu rl, CURLOPT_RETURNT RANSFER, 1);
    curl_setopt($cu rl, CURLOPT_HEADER, 1);
    curl_setopt($cu rl, CURLOPT_MAXREDI RS, 2);
    curl_setopt($cu rl, CURLOPT_FOLLOWL OCATION, 1);
    >
    curl_setopt($cu rl, CURLOPT_POST, 1);
    curl_setopt($cu rl, CURLOPT_POSTFIE LDS, array('clown'=> 'bozo') );
    >
    curl_setopt($cu rl, CURLOPT_URL, 'http://www.somesite.co m/form.htm');
    $response = curl_exec($curl );
    >
    echo $response; // this echos nothing and when saved to a file the
    file is 0 bytes...

    Comment

    • zorro

      #3
      Re: posting with cURL problem

      zorro wrote:
      it doesn't work :
      curl_setopt($cu rl, CURLOPT_POSTFIE LDS, array('clown'=> 'bozo') );
      Benjamin wrote:
      CURLOPT_POSTFIE LDS only takes a string as it's parameter

      I didn't find any specification stating that it takes a string
      parameter.


      There are dozens of examples where a string or an array is used and i
      have also tried both successfully. The problem is only on this one
      particular web site I'm posting to.

      I need to use an array and not a string because for posting a file
      upload one needs to use this syntax:
      curl_setopt($cu rl, CURLOPT_POSTFIE LDS,
      array('mypic'=> "@path/to/image.gif") );

      I tested that this works on my localhost and online testing server.

      Comment

      • zorro

        #4
        Re: posting with cURL problem

        Ok I found the reason, hope this helps someone eventually:

        You can see headers sent by cURL by using

        $mydebug = fopen('debug.tx t','w');
        curl_setopt($cu rl, CURLOPT_STDERR, $mydebug);
        curl_setopt($cu rl, CURLOPT_VERBOSE , 1);

        I noticed those headers were different when posting with an array.
        Namely, there is a "Expect: 100-continue header" which basically tells
        the server that some content will be posted but only if the server
        responds back with "HTTP/1.1 100 Continue" code. Why on the web site I
        was posting to the continuing doesn't happen automatically like on my
        testing servers i don't know. I tried setting headers like "Connection :
        keep-alive" but it didn't help. What did work though was removing the
        "Expect" header :

        curl_setopt($cu rl, CURLOPT_HTTPHEA DER, array('Expect:' ));

        So now cURL doesn't ask permission to post first but just posts
        directly.

        Comment

        • Benjamin

          #5
          Re: posting with cURL problem


          zorro wrote:
          Ok I found the reason, hope this helps someone eventually:
          >
          You can see headers sent by cURL by using
          >
          $mydebug = fopen('debug.tx t','w');
          curl_setopt($cu rl, CURLOPT_STDERR, $mydebug);
          curl_setopt($cu rl, CURLOPT_VERBOSE , 1);
          >
          I noticed those headers were different when posting with an array.
          Namely, there is a "Expect: 100-continue header" which basically tells
          the server that some content will be posted but only if the server
          responds back with "HTTP/1.1 100 Continue" code. Why on the web site I
          was posting to the continuing doesn't happen automatically like on my
          testing servers i don't know. I tried setting headers like "Connection :
          keep-alive" but it didn't help. What did work though was removing the
          "Expect" header :
          >
          curl_setopt($cu rl, CURLOPT_HTTPHEA DER, array('Expect:' ));
          >
          So now cURL doesn't ask permission to post first but just posts
          directly.
          thank you for noticing that

          Comment

          Working...