如何在Rest API PHP中发送数组

时间:2019-08-17 10:49:02

标签: php arrays rest api

我想用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"); 

因为驱动程序参数需要数组数据值 当我运行代码时,向我返回错误”:[[驱动程序数据中有错误!]]

2 个答案:

答案 0 :(得分:1)

您需要使用参数数组构建查询字符串

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

答案 1 :(得分:1)

$data=array('mobile'=>'+123456','title'=>"MR",'firstName'=>'john','lastName'=>'wilet');
$drv=json_encode($data);
curl_setopt($process, CURLOPT_POSTFIELDS, "&driver=$drv");