我需要完全发送:

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

我希望你们可以帮助我一个例子,最好的问候!

有帮助吗?

解决方案

经过大量的研究,我发现如何做到这一点...

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

* .-您可以看到响应取消关注此代码段

//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); 
.

如果您有任何疑问,请立即让我感到乐意帮助您;)

其他提示

如果您想在PHP中执行此操作,我会使用卷曲。以下是未经测试的,所以没有保证它是正确的:

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top