php CURLOPT_POSTFIELDS How to pass multiple parameters
PHP中文网
PHP中文网 2017-05-31 10:33:46
0
2
791

As the title says,

$data = $textArray['content'];// 是个字符串
$params = array('top_k'=>10);
$ch = curl_init();
curl_setopt_array($ch,
    CURLOPT_POSTFIELDS => json_encode($data,$params,JSON_UNESCAPED_UNICODE),
));// 这里其他部分省略了

This is what I wrote

But when printing json_encode($data,$params,JSON_UNESCAPED_UNICODE), it is found to be empty

Then write CURLOPT_POSTFIELDS => json_encode($data,JSON_UNESCAPED_UNICODE),json_encode($params,JSON_UNESCAPED_UNICODE),
This way we find that $params is not passed

Please answer, thank you

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
小葫芦

Method 1

$data = $textArray['content'];// 是个字符串
$params = array('top_k'=>10);

$array = [];
$array['data'] = $data;
$array['params'] = $params;

$ch = curl_init();
curl_setopt_array($ch,
    CURLOPT_POSTFIELDS => json_encode($array,JSON_UNESCAPED_UNICODE),
));
//code.....

Method 2

$data = $textArray['content'];// 是个字符串
$params = array('top_k'=>10);

$params['data'] = $data;

$ch = curl_init();
curl_setopt_array($ch,
    CURLOPT_POSTFIELDS => json_encode($params,JSON_UNESCAPED_UNICODE),
));
//code.....

Variation of method 2

$data = $textArray['content'];// 是个字符串

$params = array('top_k'=>10,'data'=>$data);

$ch = curl_init();
curl_setopt_array($ch,
    CURLOPT_POSTFIELDS => json_encode($params,JSON_UNESCAPED_UNICODE),
));
//code.....

Method 3 Simple version

$ch = curl_init();
curl_setopt_array($ch,
    CURLOPT_POSTFIELDS => json_encode(array('top_k'=>10,'data'=>$textArray['content']),JSON_UNESCAPED_UNICODE),
));
//code.....

That’s it. . . .

I find that you don’t even know the basics of php. .

某草草
curl_setopt_array($ch,
    CURLOPT_POSTFIELDS => json_encode($data,$params,JSON_UNESCAPED_UNICODE),
));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json'
);
//接收
$data  = file_get_contents('php://input');
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template