我正在尝试通过来自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
)
)
请帮忙吗? 非常感谢!
答案 0 :(得分:1)
所以这里有几点需要注意。有点难以弄清楚你试图从中检索id的数据是什么。
/消息/ Y2lzY29zcGFyazovL3VzL01FU1NBR0UvMzIzZWUyZjAtOWFhZC0xMWU1LTg1YmYtMWRhZjhkNDJlZjlj
$data->id
修改强>
非常简单,试试吧。
$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);
如果有任何改变,请告诉我。