PHP中的Pushover API错误发送消息

时间:2014-01-17 14:27:38

标签: php api

所以我正在关注PHP的Pushover FAQ示例:

<?php
curl_setopt_array($ch = curl_init(), array(
  CURLOPT_URL => "https://api.pushover.net/1/messages.json",
  CURLOPT_POSTFIELDS => array(
  "token" => "APP_TOKEN",
  "user" => "USER_KEY",
  "message" => "hello world",
)));
curl_exec($ch);
curl_close($ch);
?>

该示例效果很好,但如果我尝试将消息作为变量发送,如:

"message" => $variable,

它给我一个错误,说我无法发送空白消息。 我想这是一个语言相关的问题。如何将变量分配给数组“message”?

谢谢。

2 个答案:

答案 0 :(得分:1)

可能Curl存在问题,您可以使用此函数将数组数据发布到api.pushover。

function sendApiPushover(){
$url  = 'https://api.pushover.net/1/messages.json';
$data = array(
            "token" => "APP_TOKEN",
            "user" => "USER_KEY",
            "title" => "John",
            "message" => "hello world"
        );

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
        )
    );

$context = stream_context_create($options);
$result  = file_get_contents($url, false, $context);

return $result;
}

答案 1 :(得分:0)

您的变量$ message似乎是空的。最好在运行此脚本之前检查:

<?php
if(!empty($message)){
curl_setopt_array($ch = curl_init(), array(
  CURLOPT_URL => "https://api.pushover.net/1/messages.json",
  CURLOPT_POSTFIELDS => array(
  "token" => "APP_TOKEN",
  "user" => "USER_KEY",
  "message" => $message,
)));
curl_exec($ch);
curl_close($ch);
}
?>