search

Home  >  Q&A  >  body text

php CURLOPT_POSTFIELDS How to pass multiple parameters

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中文网2765 days ago792

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-05-31 10:35:46

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

    reply
    0
  • 某草草

    某草草2017-05-31 10:35:46

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

    reply
    0
  • Cancelreply