49

I am wondering how do I make this code support arrays? At the moment the images array only seems to send the first value.

Here is my code:

<?php
//extract data from the post
extract($_POST);

//set POST variables
$url = 'http://api.example.com/api';
$fields = array(
            'username' => "annonymous",
            'api_key' => urlencode("1234"),
            'images[]' => urlencode(base64_encode('image1')),
            'images[]' => urlencode(base64_encode('image2'))
        );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);
echo $result;

//close connection
curl_close($ch);
?>

and this is what is received at the api

VAR: username = annonymous
VAR: api_key = 1234
VAR: images = Array
array(3) { 
         ["username"]=> string(10) "annonymous" 
         ["api_key"]=> string(4) "1234" 
         ["images"]=> array(1) { // this should contain 2 strings :( what is happening?
                               [0]=> string(8) "aW1hZ2Uy" 
                               } 
         }

What is happening to the second value in images[]?

Goulash
  • 3,708
  • 6
  • 29
  • 48

2 Answers2

103

You are just creating your array incorrectly. You could use http_build_query:

$fields = array(
            'username' => "annonymous",
            'api_key' => urlencode("1234"),
            'images' => array(
                 urlencode(base64_encode('image1')),
                 urlencode(base64_encode('image2'))
            )
        );
$fields_string = http_build_query($fields);

So, the entire code that you could use would be:

<?php
//extract data from the post
extract($_POST);

//set POST variables
$url = 'http://api.example.com/api';
$fields = array(
            'username' => "annonymous",
            'api_key' => urlencode("1234"),
            'images' => array(
                 urlencode(base64_encode('image1')),
                 urlencode(base64_encode('image2'))
            )
        );

//url-ify the data for the POST
$fields_string = http_build_query($fields);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);
echo $result;

//close connection
curl_close($ch);
?>
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
Benjamin Powers
  • 3,186
  • 2
  • 18
  • 23
  • I've tried that but then it just sends `images = array`, array being a string :/ `var_dump($_POST['images']` returns `string(5) "Array" 1` – Goulash Nov 28 '12 at 02:44
  • hmm, that's strange. Did you use the http_build_query? If I echo the $fields_string above, it gives me username=annonymous&api_key=1234&images%5B0%5D=aW1hZ2Ux&images%5B1%5D=aW1hZ2Uy – Benjamin Powers Nov 28 '12 at 02:45
  • I get `username=annonymous&api_key=1234&images=Array` no I don't think I use `http_build_query` ? – Goulash Nov 28 '12 at 02:47
  • If you are really wanting to build that query string manually, you can. However, http_build_query will make your "url-ify the data for the POST" section unnecessary. – Benjamin Powers Nov 28 '12 at 02:48
  • ohhhh, I see, :P now it works with `$fields_string = http_build_query($fields);` thanks so much :D – Goulash Nov 28 '12 at 02:49
  • thanks for the answerl!! I was able to form url with this but now how am i supposed to parse this url at the server(which is using cherrypy)? – proprius Jul 22 '16 at 11:26
  • 3
    extract($_POST); is a security gap! Do not use it – Jakob Alexander Eichler Oct 07 '16 at 10:26
  • I'm programming in PHP about 8 years but I see this method now. This is awesome method thank you... – kodmanyagha Jul 11 '17 at 19:01
  • to add to what jakob said, you shouldn't use extract on $_POST or $_GET without the prefix options. using them with prefix should be fine. but ofcourse do your own research, security aspects keep changing! – Ranjit Chawla Nov 03 '20 at 06:36
-2
    $ch = curl_init();

    $data = array(
        'client_id' => 'xx',
        'client_secret' => 'xx',
        'redirect_uri' => $x,
        'grant_type' => 'xxx',
        'code' => $xx,
    );

    $data = http_build_query($data);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_URL, "https://example.com");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);

    $output = curl_exec($ch);
mate.gvo
  • 1,093
  • 14
  • 20