qq_15568321 2023-05-09 14:36 采纳率: 0%
浏览 72
已结题

PHP通过CURL请求restAPI出错,如何解决?

PHP通过CURL请求restAPI出错


      $runCommand = array('sun at 13:16', 'Full 1st Sat at 21:00');
      //创建客户端
      $data = array(
          'name' => 's050 901',
          'description' => 'description  111111',
          'enabled' => 'yes',
          'runCommand' => $runCommand
      );
   $result_create=$this->getCommonModel()->createSchedule($data);

请求方法:

   public function createSchedule($data){
        //获取token
        $token=$this->getAuthToken();

        //获取配置信息
        $rest_config = $this->getServiceLocator()->get('ModuleManager')->getModule('Common')->getConfig();
        $restinfo=$rest_config['rest_api'];

        $ch = curl_init();
        // 设置Bareos REST API的URL
        curl_setopt($ch, CURLOPT_URL, $restinfo['rest_url'].'/configuration/schedules');
        // 设置要提交的数据

        $data = json_encode($data);

        // 设置CURL选项
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data),
            'Authorization: Bearer ' . $token
        ));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        // 发送请求并获取响应
        $response = curl_exec($ch);

        // 关闭CURL会话
        curl_close($ch);

        return $response;
    }

最终服务端生成的命令是\u0027configure add schedule name=s050901 description=description111111 runCommand=sunat13:16,Full1stSatat21:00 enabled=yes\u0027

正确的应该是\u0027configure add schedule name=s050901 description=description111111 runCommand="sun at 13:16,Full 1st Satat 21:00" enabled=yes\u0027
而且name跟description这两个参数值里面的空格也丢了
上面的代码哪里出了问题,chatgpt别来凑热闹,必举报

  • 写回答

3条回答 默认 最新

  • churuxu 2023-05-09 15:32
    关注

    你先本地把值打印出来看看,排查是客户端代码问题,还是服务端代码问题

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月10日
  • 修改了问题 5月10日
  • 修改了问题 5月9日
  • 赞助了问题酬金15元 5月9日
  • 展开全部