3

I have an array of data comes from $_POST and I want to send them by curl to another page.

 curl_setopt($s,CURLOPT_POST,true);
 curl_setopt($s,CURLOPT_POSTFIELDS,$this->_postFields);

$this->_postFields must be an string like a=2&b=t right?
so if I want to send array of data with curl to another page I must turn array to query string right?

How should I do it with PHP?

♦I tried serialize() and unserialize() but thier format is not same as query string right?
so what should I do? (I need something like .serialize() in jQuery that work on array not FORM)

♦ And the destination path is not under my control and the $_POST in the destination should be as $_POST not as its base64 encoded so I can't use such codes.

$array = array(1,2,3);
$encoded = json_encode($array);
$decoded = json_decode($encoded);
print_r($decoded);

Any Ideas?

thanks in advance.

3
  • Please let me know if the question is not clear or even if I should delete the question. Instead of just putting -1.thanks again. Commented Jan 16, 2014 at 20:30
  • For a PHP solution, http_build_query() might be helpful: PHP Function To Build Query String From Array Commented Jan 16, 2014 at 20:37
  • @rafaelsoufraz - thanks but the destination path is not under my control and the $_POST in the destination should be as $_POST not as its base64 encoded so I can't use such codes.` $array = array(1,2,3); $encoded = json_encode($array); $decoded = json_decode($encoded); print_r($decoded);` Commented Jan 16, 2014 at 20:38

3 Answers 3

5

You can use http_build_query:

curl_setopt($s, CURLOPT_POSTFIELDS, http_build_query($this->_postFields));

NOTICE

It looks beauty, but to use this approach be careful about url encoding .Look next:

$_POST["some value"]='value1'; // " " between
$_POST["next value"]='value2'; // " " between

$url = http_build_query($_POST);

echo $url;

// OUTPUT 
some+value=value1&next+value=value2

Of course, after sending this $url we will not get expected variables from $_POST.

Sign up to request clarification or add additional context in comments.

Comments

5

I think you can just do this:

curl_setopt($s, CURLOPT_POSTFIELDS, http_build_query($this->_postFields));

Comments

1

If I understand, you can use json.

$array = array(1,2,3);
$encoded = json_encode($array);
$decoded = json_decode($encoded);
print_r($decoded); //Look to it

If is not it, can be it:

$array = array(1,2,3);
echo http_build_query($array); //Look to it

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.