经过大量研究,我发现了如何做到这一点......
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);
如果您有任何问题,请让我现在很乐意为您提供帮助;)