user2502659
user2502659

Reputation: 1

Pushover API error sending message in PHP

So I'm following the Pushover FAQ example for PHP:

<?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);
?>

That example works great, but if I try to send the message as a variable like:

"message" => $variable,

It gives me an error saying that I can't send a blank message. I guess it's a language related problem. How can I assign a variable to the array "message"?

Thank you.

Upvotes: 0

Views: 1265

Answers (2)

Dani Hadi
Dani Hadi

Reputation: 21

Maybe there is a problem with Curl, you can use this function to post array data into 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;
}

Upvotes: 1

hugo jan
hugo jan

Reputation: 176

It seems that your variable $message is empty. Better to check before running this script by:

<?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);
}
?>

Upvotes: 0

Related Questions