Come inviare json post in cakephp
Domanda
Ho bisogno di inviare esattamente questo:
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"
}
}
.
Non ho mai fatto qualcosa del genere e ho fatto un sacco di ricerche ma non riesco a trovare come inviare quei parametri a quell'URL
Spero che voi ragazzi possa aiutarmi con un esempio per favore, i migliori saluti!
Soluzione
Dopo un sacco di ricerche, scopro come farlo ...
1.- Usa
App::uses('HttpSocket', 'Network/Http'); // you should put this on your controller
.
2.- Questo sulla tua funzione
$HttpSocket = new HttpSocket();
.
3.- Ecco i dati che si desidera inviare tramite posta (in questo esempio utilizzerò le variabili che ho usato. Puoi sostituirle, aggiungere altro o eliminare alcuni .. Dipende dalle informazioni che desideri Per inviare)
$data = array(
"api_key" => "API KEY",
"user_id" => $idUser,
"event" => "other",
"extra" => array(
"course" => $course,
"price"=> $price )
);
.
3.- Imposta le intestazioni
$request = array(
'header' => array('Content-Type' => 'application/json',
),
);
.
4.-json_code IT
$data = json_encode($data);
.
5.- Dove stai inviando il post a?, quali dati?, Tipo di richiesta?, fallo in questo modo
$response = $HttpSocket->post('http://api.yourweburl.com/api/', $data, $request);
.
* .- Puoi vedere la risposta che si complimenta questo snippet
//pr($response->body());
.
* .- Infine se vuoi reindirizzare da qualche parte dopo che tutto è fatto .. fallo in questo modo ...
$this->redirect(array('action' => 'index'));
.
Dovresti avere qualcosa di simile.
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'));
.
}
È così che chiami questa funzione da un'altra funzione (solo nel caso)
$this->actiontooutbound($idUser, $course, $price);
.
Se avete domande, fammi ora sarò felice di aiutarti;)
Altri suggerimenti
Se vuoi farlo in PHP, userei il ricciolo.Quanto segue è non testato, quindi nessuna garanzia che è corretta:
$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;
.