0

I have an issue with libcurl with php. I was sending post data using a string like that:

    $post="var1=val1&var2=val2";
    curl_setopt($this->ch, CURLOPT_URL, $url);
    curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($this->ch, CURLOPT_POST, 1);
    curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded','Accept-Encoding: gzip'));
    curl_setopt($this->ch,CURLOPT_ENCODING , "gzip");
    curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);

And it worked perfectly, but I would like to change to use an array instead like that:

    $post=array();
    $post['var1']="val1";
    $post['var2']="val2";

    curl_setopt($this->ch, CURLOPT_URL, $url);
    curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($this->ch, CURLOPT_POST, 1);
    curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded','Accept-Encoding: gzip'));
    curl_setopt($this->ch,CURLOPT_ENCODING , "gzip");
    curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);

but the server doesn't seems to receive the same thing, is there something I'm missing ?

Entretoize
  • 2,124
  • 3
  • 23
  • 44
  • Nigel, you mark as duplicate to a question asking how to send post data when I'm giving to way of doing it. Even if the answer is good, the question is not a duplicate. – Entretoize Nov 02 '18 at 06:21

1 Answers1

0

The note here says:

Note: Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data, while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded.

This may be the reason of the other site doesn't recognize the same way. As an workaround, you can transform the array to string using $post = http_build_query($post);

Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29