首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pushover API在PHP中发送消息时出错

Pushover API在PHP中发送消息时出错
EN

Stack Overflow用户
提问于 2014-01-17 22:27:38
回答 2查看 1.1K关注 0票数 0

因此,我遵循PHP的Pushover常见问题示例:

代码语言:javascript
复制
<?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);
?>

这个示例运行得很好,但是如果我尝试将消息作为变量发送,比如:

代码语言:javascript
复制
"message" => $variable,

它会给我一个错误,告诉我不能发送空白消息。我猜这是一个与语言相关的问题。如何将变量赋值给数组"message"?

谢谢。

EN

回答 2

Stack Overflow用户

发布于 2017-02-23 15:35:43

也许Curl有问题,你可以使用这个函数将数组数据post到api.pushover中。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2014-01-17 22:31:02

您的变量$message似乎为空。在运行此脚本之前,最好通过以下方式进行检查:

代码语言:javascript
复制
<?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);
}
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21188213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档