php CURLOPT_POSTFIELDS Bagaimana untuk lulus berbilang parameter
PHP中文网
PHP中文网 2017-05-31 10:33:46
0
2
789

Seperti tajuknya,

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

Ini yang saya tulis

Tetapi apabila mencetak json_encode($data,$params,JSON_UNESCAPED_UNICODE), ia didapati kosong

Kemudian tulis CURLOPT_POSTFIELDS => json_encode($data,JSON_UNESCAPED_UNICODE),json_encode($params,JSON_UNESCAPED_UNICODE),
Dengan cara ini anda mendapati $params tidak diluluskan

Sila jawab, terima kasih

PHP中文网
PHP中文网

认证高级PHP讲师

membalas semua(2)
小葫芦

Kaedah 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.....

Kaedah 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.....

Variasi kaedah 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.....

Kaedah 3 Versi ringkas

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

Itu sahaja. . . .

Saya dapati anda tidak tahu asas 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');
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan