从webhook中检索JSON数据

时间:2016-03-15 14:29:58

标签: php json curl

我正在尝试通过来自JSON Webhook的cURL GET从JSON中检索“text”属性。为了获取信息“文本”,我的应用程序必须获取资源[“id”:“],这是一个变量。 JSON如下所示:

{
"id":"Y2lzY29zcGFyazovL3VzL01FU1NBR0UvMzIzZWUyZjAtOWFhZC0xMWU1LTg1YmYtMWRhZjhkNDJlZjlj",   
"roomId":"Y2lzY29zcGFyazovL3VzL1JPT00vY2RlMWRkNDAtMmYwZC0xMWU1LWJhOWMtN2I2NTU2ZDIyMDdi",
"personId":"Y2lzY29zcGFyazovL3VzL1BFT1BMRS9lM2EyNjA4OC1hNmRiLTQxZjgtOTliMC1hNTEyMzkyYzAwOTg",    
"personEmail":"person@example.com",
"text":"Something interesting and potentially sensitive",
"created":"2016-03-15T07:33:56.767Z"    
}

我正在尝试部署cURL GET / message / {id},但是,我正在实现的cURL无效。遵循代码:

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

跟随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] => person@example.com
            [created] => 2016-03-15T14:19:20.040Z
        )

)

请帮忙吗? 非常感谢!

1 个答案:

答案 0 :(得分:1)

所以这里有几点需要注意。有点难以弄清楚你试图从中检索id的数据是什么。

  1. 您的网址看起来不对,您说您正在向/ messages / {id}进行GET。 {id},表示您将其替换为您的id值。这是API文档的标准语法。因此,如果我使用第一个JSON块中的ID,则URL应如下所示。
  2. /消息/ Y2lzY29zcGFyazovL3VzL01FU1NBR0UvMzIzZWUyZjAtOWFhZC0xMWU1LTg1YmYtMWRhZjhkNDJlZjlj

    1. 如果您尝试从最顶层的JSON对象中检索ID,则只需使用以下内容。
    2. $data->id

      1. 这是更多语义,但您不需要使用CURLOPT_CUSTOMREQUEST => “GET”,如果你只是在做一个标准的GET请求。
      2. 修改

        非常简单,试试吧。

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

        如果有任何改变,请告诉我。