猿问

如何在rest api php中发送数组

我想在php中发送带有rest api的数组,但我不知道该怎么做


我尝试使用 json 而没有 json 但没有回应我


$drv = array('mobile'=>'+123456', 'title'=>"MR", 'firstName_en'=>'john', 'lastName_en'=>'wilet');

curl_setopt($process, CURLOPT_POSTFIELDS, "checkoutStation=xxx&checkoutDate=20190826&checkoutTime=0900&checkinStation=xxx&checkinDate=20190827&checkinTime=0900&email=info@site.net&driver=$drv");

也试试


$drv = array('mobile'=>'+123456', 'title'=>"MR", 'firstName_en'=>'john', 'lastName_en'=>'wilet');

$drv=json_encode($drv);

curl_setopt($process, CURLOPT_POSTFIELDS, "checkoutStation=xxx&checkoutDate=20190826&checkoutTime=0900&checkinStation=xxx&checkinDate=20190827&checkinTime=0900&email=info@site.net&driver=$drv"); 

因为驱动程序参数在我运行代码时需要一个数组数据值返回错误":["驱动程序数据错误!"] 给我


潇潇雨雨
浏览 165回答 2
2回答

慕田峪7331174

您需要使用参数数组构建查询字符串$params = array(        'checkoutStation' => 'xxx',        'checkoutDate' => '20190826',        'checkoutTime' => '0900',        'checkinStation' => 'xxx',        'checkinDate' => '20190827',        'checkinTime' => '0900',        'email' => 'info@site.net',        'driver' => array(            'mobile'=>'+123456',            'title'=>"MR",             'firstName_en'=>'john',             'lastName_en'=>'wilet'        )    );curl_setopt($process, CURLOPT_POSTFIELDS, http_build_query($params));

一只斗牛犬

$data=array('mobile'=>'+123456','title'=>"MR",'firstName'=>'john','lastName'=>'wilet');$drv=json_encode($data);curl_setopt($process, CURLOPT_POSTFIELDS, "&driver=$drv");
随时随地看视频慕课网APP
我要回答