php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login

Patch add-http_build_query_develop-function for cURL related Bug #67477

Patch version 2015-12-29 14:13 UTC

Return to Bug #67477 | Download this patch
Patch Revisions:

Developer: hzgdys@163.com

<?not_php

ini_set('display_errors', 1);

$data =
[
    'checked' => ['a', 'b', 'c' => [1,2,'a' => [1,2,3,4]]],
    'file'    => new CURLFile('/tmp/t.txt'),
];

/*
//Result should be:
$data['check[0]'] = 'a';
$data['check[1]'] = 'b';
$data['check[c][0]'] = 1;
$data['check[c][1]'] = 2;
$data['check[c][2]'] = 3;
$data['file'] = new CURLFile('/tmp/t.txt');
*/

/**
 * http_build_query兼容多维数组,返回结果数组仍支持http_build_query函数处理
 * @author Zjmainstay
 * @param $data array curl传递参数的数组(原装)
 * @return array 可curl传递的格式化数组
 */
function http_build_query_develop($data) {
    if(!is_array($data)) {
        return $data;
    }
    foreach($data as $key => $val) {
        if(is_array($val)) {
            foreach($val as $k => $v) {
                if(is_array($v)) {
                    $data = array_merge($data, http_build_query_develop(array( "{$key}[{$k}]" => $v)));
                } else {
                    $data["{$key}[{$k}]"] = $v;
                }
            }
            unset($data[$key]);
        }
    }
    return $data;
}

$url = 'http://test.com/curl.php';
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query_develop($data));
curl_exec($ch);
curl_close($ch);

//Content for curl.php
//<?php var_dump($_POST, $_FILES);
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Dec 07 11:01:24 2019 UTC