将PhoneNumber字段作为数组传递

时间:2019-01-08 12:40:31

标签: php laravel

我在我的应用程序中使用了在线laravel api文档。当我在浏览器中运行代码时,出现错误“将电话号码字段作为数组传递”。但是number字段已经作为数组字段传递。我的代码下面可能缺少什么?预先感谢

3.2.2

当我返回$ variables时,我得到了响应

public function testAPI(Request $request)
    {
        $on_call_back = 'https://learntoday.co.uk/var';
        $id = '*****';
        $url = $on_call_back.'?key='.$id;
        $variables = [
           'phoneNumber' => ['44234200234','44234242002'],
           'from' => 'world',
           'content' => 'I love to code',
        ];

        $ch = curl_init();
        $headers = array();
        $headers[] = "Content-Type: application/json";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($variables));
        $result = curl_exec($ch);
        $result = json_decode($result, TRUE);
        curl_close($ch);
    }

4 个答案:

答案 0 :(得分:1)

您的数组语法似乎是错误的,您是否尝试过将每个数字都用单引号引起来,如下所示:

 'phoneNumber' => ['44234200234', '44234242002'],

答案 1 :(得分:0)

您应该尝试以下操作:

$variables = [
           'phoneNumber' => ['44234200234','44234242002'],
           'from' => 'world',
           'content' => 'I love to code',
        ];

更新后的答案

public function testAPI(Request $request)
    {
        $on_call_back = 'https://learntoday.co.uk/var';
        $id = '*****';
        $url = $on_call_back.'?key='.$id;
        $variables = [
           'phoneNumber' => ['44234200234','44234242002'],
           'from' => 'world',
           'content' => 'I love to code',
        ];

        $ch = curl_init();
        $headers = array();
        $headers[] = "Content-Type: application/json";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($variables));
        $result = curl_exec($ch);
        $result = json_decode($result, TRUE);
        curl_close($ch);
    }

答案 2 :(得分:0)

请尝试发送$ variable,如下代码

$variables = array(
           'phoneNumber' => array('44234200234','44234242002'),
           'from' => 'world',
           'content' => 'I love to code',
        );

答案 3 :(得分:0)

更改

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($variables));

curl_setopt($ ch,CURLOPT_POSTFIELDS,$ variables);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($variables));

您也可以执行curl_setopt($ch, CURLOPT_POST, 1);而不是CURLOPT_CUSTOMREQUEST