Php 如何在cURL POST请求中使用数组
我想知道如何使这段代码支持数组?此时,Php 如何在cURL POST请求中使用数组,php,post,curl,Php,Post,Curl,我想知道如何使这段代码支持数组?此时,images数组似乎只发送第一个值 这是我的密码: <?php //extract data from the post extract($_POST); //set POST variables $url = 'http://api.example.com/api'; $fields = array( 'username' => "annonymous", 'api_key' => url
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);
?>
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' => 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);
?>
在声明$fields
数组后,执行打印($fields)
操作,很明显这不起作用(提示:只有一个images[]
键值对)。好的,我明白了,我应该怎么做?我已经尝试过了,但它只发送images=array
,数组是字符串:/var\u($\u POST['images']
返回string(5)“数组”1
hmm,这很奇怪。你使用了http\u build\u查询吗?如果我回显上面的$fields\u字符串,它会给我username=annonymous&api\u key=1234&images%5B0%5D=aW1hZ2Ux&images%5B1%5D=aW1hZ2UyI getusername=annonymous&api\u\u key=1234&images=Array
不,如果你真的想构建您可以手动查询字符串。但是,http\u build\u查询将使“url为帖子提供数据”部分变得不必要。提取($\u POST);是一个安全漏洞!请勿使用它
$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);
?>
$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);