Fix #393: Add examples for POSTing data with and without indexes

This commit is contained in:
Zach Borboa
2016-09-21 23:58:36 -07:00
parent 52f4df9cfe
commit 371a09b6e9
3 changed files with 57 additions and 0 deletions
@@ -0,0 +1,12 @@
<?php
require __DIR__ . '/vendor/autoload.php';
use \Curl\Curl;
// curl "https://httpbin.org/post" -d "foo[0]=bar&foo[1]=baz"
$curl = new Curl();
$curl->post('https://httpbin.org/post', array(
'foo[0]' => 'bar',
'foo[1]' => 'baz',
));
@@ -0,0 +1,14 @@
<?php
require __DIR__ . '/vendor/autoload.php';
use \Curl\Curl;
// curl "https://httpbin.org/post" -d "foo[]=bar&foo[]=baz"
$curl = new Curl();
$curl->post('https://httpbin.org/post', array(
'foo' => array(
'bar',
'baz',
),
));
@@ -0,0 +1,31 @@
<?php
require __DIR__ . '/vendor/autoload.php';
use \Curl\Curl;
// curl "https://httpbin.org/post" -d "foo=bar&foo=baz"
function http_build_query_without_indexes($query) {
$array = array();
foreach ($query as $key => $value) {
$key = rawurlencode($key);
if (is_array($value)) {
foreach ($value as $v) {
$v = rawurlencode($v);
$array[] = $key . '=' . $v;
}
} else {
$value = rawurlencode($value);
$array[] = $key . '=' . $value;
}
}
return implode('&', $array);
}
$curl = new Curl();
$curl->post('https://httpbin.org/post', http_build_query_without_indexes(array(
'foo' => array(
'bar',
'baz',
),
)));