【问题标题】:how to send json post in cakephp如何在cakephp中发送json帖子
【发布时间】:2014-01-09 21:48:24
【问题描述】:

我需要发送这个:

POST http://api.outbound.io/api/v1/identify
Content-Type: application/json
{
 "api_key": "MY_API_KEY",
 "user_id": "MY_UNIQUE_USER_ID",
 "traits" : {
    "email" : "dhruv@outbound.io",
    "name" : "Dhruv Mehta",
    "phone" : "650xxxyyyyy"
 }
}

我从来没有做过这样的事情,我已经做了很多研究,但我找不到如何将这些参数发送到那个 URL

我希望你们能帮我举个例子,最好的问候!

【问题讨论】:

    标签: json api cakephp post outbound


    【解决方案1】:

    经过大量研究,我找到了方法......

    1.- 使用

    App::uses('HttpSocket', 'Network/Http'); // you should put this on your controller
    

    2.- 这在你的功能上

    $HttpSocket = new HttpSocket(); 
    

    3.- 这是您要通过 POST 发送的数据(在此示例中,我将使用我使用过的变量..您可以替换它们,添加更多或删除一些.. 这取决于您想要的信息发送)

    $data = array(
               "api_key" => "API KEY",
               "user_id" => $idUser,
               "event" => "other",
               "extra" => array(
                              "course" => $course,
                              "price"=> $price )
                   );
    

    3.- 你设置标题

    $request = array(
            'header' => array('Content-Type' => 'application/json',
            ),
        );
    

    4.-json_encode吧

     $data = json_encode($data);
    

    5.- 你要将帖子发送到哪里?,哪些数据?,请求类型?,这样做

    $response = $HttpSocket->post('http://api.yourweburl.com/api/', $data, $request);
    

    *.- 你可以看到取消注释这个 sn-p 的响应

    //pr($response->body());
    

    *.- 最后,如果你想在一切完成后重定向到某个地方..这样做......

    $this->redirect(array('action' => 'index'));
    

    你应该有这样的东西。

    public function actiontooutbound($idUser, $course, $price){
     $HttpSocket = new HttpSocket();
    
        $data = array(
               "api_key" => "API KEY",
               "user_id" => $idUser,
               "event" => "other",
               "extra" => array(
                              "course" => $course,
                              "price"=> $price )
                   );
    
        $request = array(
            'header' => array(
                'Content-Type' => 'application/json',
            ),
        );
        $data = json_encode($data);
        $response = $HttpSocket->post('http://api.outbound.io/api/v1/track', $data, $request);
       // pr($data);
        //pr($response->body());
       $this->redirect(array('action' => 'index'));     
    

    }

    这是您从另一个函数调用此函数的方式(以防万一)

    $this->actiontooutbound($idUser, $course, $price); 
    

    如果您有任何问题,请立即告诉我,我很乐意为您提供帮助;)

    【讨论】:

      【解决方案2】:

      如果您想在 PHP 中执行此操作,我会使用 curl。以下内容未经测试,因此不保证其正确性:

      $json = array(
          'api_key' => 'My_API_KEY',
          'user_id' => 'MY_UNIQUE_USER_ID',
          'traits' => array(
                'email' =< 'dhruv@outbound.io',
                'name' => 'Dhrub Mehta',
                'phone' => '650xxxyyyyy'
           )
      );
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type: application/json'));
      curl_setopt($ch, CURLOPT_URL, 'http://api.outbound.io/api/v1/identify');
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($json));
      curl_setopt($ch, CURLOPT_POST, 1);
      
      $results = curl_exec($ch);
      if (curl_errno($ch)) {
          debug(curl_error($ch));
      } else {
          curl_close($ch);
      }
      
      return $results;
      

      【讨论】:

        猜你喜欢
        • 2018-10-05
        • 1970-01-01
        • 2011-09-06
        • 2018-12-12
        • 2022-08-07
        • 2021-04-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多