JSON returns 一个对象,但一旦分配给一个变量就会变成一个 bool
JSON returns an object but turns into a bool once assigned to a variable
我现在正在将广播集成到 Messenger 中,遵循 Messenger 平台 API:
https://developers.facebook.com/docs/messenger-platform/send-messages/broadcast-messages
我正在按照步骤操作,广播消息已发送
下面的函数应该return
{
"broadcast_id": <BROADCAST_ID>
}
如文档中所述,相反,它看起来只是将其回显出来,当我尝试将其分配给一个变量(稍后将其记录在数据库中)时,它变成了一个布尔值并且 return是真的
public function send_message($message_creative_id, $access_token)
{
$creativeIdJSON = '{
"message_creative_id": "' . $message_creative_id . '"
}';
$api_url = 'https://graph.facebook.com/v2.11/me/broadcast_messages?access_token='.$access_token;
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $creativeIdJSON);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result);
if(isset($response))
{
return $response;
}
else
{
return false;
}
}
如何获取广播 ID 并将其分配给变量?
来自 curl_exec 的手册:
Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.
所以:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
我现在正在将广播集成到 Messenger 中,遵循 Messenger 平台 API: https://developers.facebook.com/docs/messenger-platform/send-messages/broadcast-messages
我正在按照步骤操作,广播消息已发送 下面的函数应该return
{
"broadcast_id": <BROADCAST_ID>
}
如文档中所述,相反,它看起来只是将其回显出来,当我尝试将其分配给一个变量(稍后将其记录在数据库中)时,它变成了一个布尔值并且 return是真的
public function send_message($message_creative_id, $access_token)
{
$creativeIdJSON = '{
"message_creative_id": "' . $message_creative_id . '"
}';
$api_url = 'https://graph.facebook.com/v2.11/me/broadcast_messages?access_token='.$access_token;
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $creativeIdJSON);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result);
if(isset($response))
{
return $response;
}
else
{
return false;
}
}
如何获取广播 ID 并将其分配给变量?
来自 curl_exec 的手册:
Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.
所以:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);