Frank
Frank

Reputation: 1407

Retrieving JSON data from webhook

I am trying to retrieve the “text” property from a JSON via cURL GET from a JSON Webhook. In order to get the information “text”, my app has to get the resource [“id”:”], which is a variable. The JSON is shown here below:

{
"id":"Y2lzY29zcGFyazovL3VzL01FU1NBR0UvMzIzZWUyZjAtOWFhZC0xMWU1LTg1YmYtMWRhZjhkNDJlZjlj",   
"roomId":"Y2lzY29zcGFyazovL3VzL1JPT00vY2RlMWRkNDAtMmYwZC0xMWU1LWJhOWMtN2I2NTU2ZDIyMDdi",
"personId":"Y2lzY29zcGFyazovL3VzL1BFT1BMRS9lM2EyNjA4OC1hNmRiLTQxZjgtOTliMC1hNTEyMzkyYzAwOTg",    
"personEmail":"[email protected]",
"text":"Something interesting and potentially sensitive",
"created":"2016-03-15T07:33:56.767Z"    
}

I’m trying to deploy a cURL GET /message/{id}, however, the cURL I’m implementing isn’t working. Follows the code:

$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
echo $data("messages");

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.mysite.com/v1/messages/id",
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "authorization: Bearer MzgwNjUwOGYtZTY4NC00NTIyLTgzNTctZmNiMjI1ZGMxZTdhNzhmYjFmNjYtNzgz",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
curl_close($curl);

Follows RAW BODY = 200 OK:

Array
(
    [id] => Y2lzY29zcGFyazovL3VzL1dFQkhPT0svMzdjZDEzMTktODFkMC00MjMwLWJkOTItNzEzMWE4NjU2YWIx
    [name] => Webhook
    [resource] => messages
    [event] => created
    [filter] => roomId=Y2lzY29zcGFyazovL3VzL1JPT00vODEyNDM0NzAtZTllZS0xMWU1LTllZjYtZjFmMDhjMjNjNDI5
    [data] => Array
        (
            [id] => Y2lzY29zcGFyazovL3VzL01FU1NBR0UvZTg4MjNlODAtZWFiOC0xMWU1LTg4OTYtNDU3YjU0OGEyNzJh
            [roomId] => Y2lzY29zcGFyazovL3VzL1JPT00vODEyNDM0NzAtZTllZS0xMWU1LTllZjYtZjFmMDhjMjNjNDI5
            [personId] => Y2lzY29zcGFyazovL3VzL1BFT1BMRS84MTE3NGUzOC04N2MwLTRmYzUtOTNlOC03NmEzMGY0MWJjZDA
            [personEmail] => [email protected]
            [created] => 2016-03-15T14:19:20.040Z
        )

)

Any help please? Many thanks!

Upvotes: 1

Views: 4679

Answers (1)

Matt D.
Matt D.

Reputation: 591

So there are a couple of things of note here. Its a little hard to figure out above what is the data your are trying to retrieve the id from.

  1. Your URL looks wrong, you say that you are making a GET to /messages/{id}. The {id}, means you replace that with your id value. This is standard syntax for API documentation. So if I use the ID from your first block of JSON the URL should look like.

/messages/Y2lzY29zcGFyazovL3VzL01FU1NBR0UvMzIzZWUyZjAtOWFhZC0xMWU1LTg1YmYtMWRhZjhkNDJlZjlj

  1. If you are trying to retrieve the ID from the very top JSON object you would simply need to use the following.

$data->id

  1. This is more semantcis but you don't need to use CURLOPT_CUSTOMREQUEST => "GET", if you are just doing a standard GET request.

Edit

Its pretty simple, just try this.

$webhookResponse = json_decode(file_get_contents('php://input'), true);

//Retrieve the id from the data object, via the id property
$messageId = $webhookResponse["data"]["id"];

$curl = curl_init();

//Add the messageId to the URL
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.ciscospark.com/v1/messages/" . $messageId,
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "authorization: Bearer MzgwNjUwOGYtZTY4NC00NTIyLTgzNTctZmNiMjI1ZGMxZTdhNzhmYjFmNjYtNzgz",
    "cache-control: no-cache",
    "postman-token: 8be1cfeb-2dda-a5a8-8e04-39af3bdf46e7"
  ),
));

$response = curl_exec($curl);

curl_close($curl);

Let me know if that changes anything.

Upvotes: 1

Related Questions