mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-30 04:01:47 +00:00
Fix #393: Add examples for POSTing data with and without indexes
This commit is contained in:
@@ -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',
|
||||
),
|
||||
)));
|
||||
Reference in New Issue
Block a user