簡體   English   中英

Pushover API錯誤在PHP中發送消息

[英]Pushover API error sending message in PHP

因此,我在關注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”分配變量?

謝謝。

也許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;
}

您的變量$ 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);
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM