【问题标题】:How do I use arrays in cURL POST requests如何在 cURL POST 请求中使用数组
【发布时间】:2012-11-15 19:34:43
【问题描述】:

我想知道如何使此代码支持数组?目前images 数组似乎只发送第一个值。

这是我的代码:

<?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);
?>

这是在 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" 
                               } 
         }

images[] 中的第二个值发生了什么变化?

【问题讨论】:

  • 在声明$fields 数组后执行print_r($fields),这不起作用的原因就很明显了(提示:只有一个images[] 键值对)。跨度>
  • 好的,我明白了,那我该怎么办?

标签: php post curl


【解决方案1】:

您只是错误地创建了数组。你可以使用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);

因此,您可以使用的整个代码是:

<?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);
?>

【讨论】:

  • 我试过了,但它只是发送images = array,数组是一个字符串:/ var_dump($_POST['images'] 返回string(5) "Array" 1
  • 嗯,这很奇怪。你使用了 http_build_query 吗?如果我回显上面的 $fields_string,它会给我 username=annonymous&api_key=1234&images%5B0%5D=aW1hZ2Ux&images%5B1%5D=aW1hZ2Uy
  • 我得到username=annonymous&amp;api_key=1234&amp;images=Array 不,我不认为我使用http_build_query
  • 如果您真的想手动构建该查询字符串,您可以。但是,http_build_query 将使您的“url-ify the data for the POST”部分变得不必要。
  • 提取($_POST);是安全漏洞!不要使用它
【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    相关资源
    最近更新 更多