1msg error when using sendMessage endpoint api - php

Hello I'm trying to use the 1msg API for my company to send WhatsApp messages, but I get the following error:
I'm using the correct endpoint any of you have idea to correct this bug
<?php
// Define the endpoint URL
$url = "https://api.1msg.io/$channelId/sendMessage?token=$tokenId";
// Define the message data
$data = array(
"phone" => "$num",
'body' => array(
"text" => "this is a test message.",
)
);
// Use http_build_query to format the data as url encoded string
$postdata = http_build_query($data);
// Prepare the options for the request
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => $postdata
)
);
// Create a stream context for the request
$context = stream_context_create($options);
// Send the request
$response = file_get_contents($url, false, $context);
> Print the response
print_r($response)
?>

Related

Postman post doesn't send any data

I'm testing an api using Postman but it doesnt send any data.
When I create a test using php function it works fine.
the api php
$json_str = file_get_contents("php://input");
$obj = json_decode($json_str, false);
the test php
$obj->CompanyCode= "39170";
$obj->CustomerNumber= "1800001";
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $obj ),
'header'=>array(
"Content-Type: application/json",
"Accept: application/json")
)
);
$context = stream_context_create( $options );
$result = file_get_contents( "https://www.url.com/inquiry/", false, $context );
the postman
postman header
postman body

PHP web push with no library

Is there an easy way to send out push notifications in PHP without using external libraries? Here is my current code. when executed the server gives a 400 UnauthorizedRegistration error. I do not care about how fast or efficient the process is, I am fine with sending one notification at a time.
My PHP code is:
<?php
ini_set("display_errors","1");
$endpoint = "https://fcm.googleapis.com/fcm/send/fImmyvrGiSo:APA91bFg8A4N4GLmJMhouxPfw_R3ocv44uBaNkZu_JMVjAvueISJUJTQROpVxlBnX8PIeJGwE5s1pvLAldtcW-z6WzZ0Ixus2fc3jUADGbJ2L8kAdMv56S5cSmKMsFmPwDsdd2MoCrHC";
$postdata = http_build_query(
array(
"p256dh" => "BAHBGTIPl7M1v8Ui7EaNVlauLmi_jEJOreCQ3YTRu_nWgF4k8nwP5rewGWO86-3wP8yZ86GoKjHQpK_0sls05FI=",
"auth" => "V08681gA2Gs_2x_UNFCN0g=="
)
);
$opts = array('http' =>
array(
'ignore_errors' => true,
'method' => 'POST',
'header' => 'Content-type: application/json',
'content' => $postdata
)
);
$context = stream_context_create($opts);
// here file_get_contents is used to send a POST request
$result = file_get_contents($endpoint, false, $context);
echo $result;
?>
My push subscription is:
{"endpoint":"https://fcm.googleapis.com/fcm/send/dcgCC59wHCE:APA91bHEzFoal6MXgXWt16aSdGHFAeS7D4vre99pCvzU_QWK22YFkmYdUQ5OmiLmUUbuhyiFEDHg61TrpllZ-TcaSgqXXOIcyTUf_0I-ngZ--aH2OFnEIrB2eI-ZRiArHD5LVAAz1EiM","expirationTime":null,"keys":{"p256dh":"BAHBGTIPl7M1v8Ui7EaNVlauLmi_jEJOreCQ3YTRu_nWgF4k8nwP5rewGWO86-3wP8yZ86GoKjHQpK_0sls05FI=","auth":"V08681gA2Gs_2x_UNFCN0g=="}}

Google Short URL API: Forbidden

I have what I think is correctly written code yet whenever I try and call it I'm getting permission denied from Google.
file_get_contents(https://www.googleapis.com/urlshortener/v1/url): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
This isn't a rate limit or anything as I currently have zero ever used...
I would have thought this is due to an incorrect API key but I've tried resetting it a number of times. There isn't some downtime while the API is first applied is there?
Or am I missing a header setting or something else just as small?
public function getShortUrl()
{
$longUrl = "http://example.com/";
$apiKey = "MY REAL KEY IS HERE";
$opts = array(
'http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/json",
'content' => json_encode(array(
'longUrl' => $longUrl,
'key' => $apiKey
))
)
);
$context = stream_context_create($opts);
$result = file_get_contents("https://www.googleapis.com/urlshortener/v1/url", false, $context);
//decode the returned JSON object
return json_decode($result, true);
}
It seems I need to manually specify the key in the URL
$result = file_get_contents("https://www.googleapis.com/urlshortener/v1/url?key=" . $apiKey, false, $context);
This now works. There must be something funny with how the API inspects POST for the key (or lack of doing so).
Edit: For anyone in the future this is my complete function
public static function getShortUrl($link = "http://example.com")
{
define("API_BASE_URL", "https://www.googleapis.com/urlshortener/v1/url?");
define("API_KEY", "PUT YOUR KEY HERE");
// Used for file_get_contents
$fileOpts = array(
'key' => API_KEY,
'fields' => 'id' // We want ONLY the short URL
);
// Used for stream_context_create
$streamOpts = array(
'http' =>
array(
'method' => 'POST',
'header' => [
"Content-type: application/json",
],
'content' => json_encode(array(
'longUrl' => $link,
))
)
);
$context = stream_context_create($streamOpts);
$result = file_get_contents(API_BASE_URL . http_build_query($fileOpts), false, $context);
return json_decode($result, false)->id;
}

How to send a gcm message in php without using cURL?

I'm using the following code to make HTTP POST request to the gcm server.
The code always returns "Unauthorized Error 401". I read that it is about the headers but can't figure out whats wrong.
Can anyone tell me what's wrong?
Is there any other way to send a gcm message?
Any help would be appreciated.
$api_key = "AIzaSyBhuPSdHmq6-************_qxSJr8d0";
$message = array("msg_url" => $msg_url, "msg_title" => $msg_title);
$url = 'https://android.googleapis.com/gcm/send';
$fields = array('registration_ids' => $ids,'data'=> array( "message" => $message ));
$headers = array('Authorization: key=' . $api_key,'Content-Type: application/json');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header'=> $headers ,
'method' => 'POST',
'content' => json_encode($fields),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
Tested your code with my API key. I am able to send request to GCM and getting proper response. It seems like your API Key is having the problem
<?php
$msg_url = "http://msg.com";
$msg_title = "message_title";
$ids = array('reg_id_1', 'reg_id_2');
$api_key = "AIzaSyBhuPSdHmq6-************_qxSJr8d0";
$message = array("msg_url" => $msg_url, "msg_title" => $msg_title);
$url = 'https://android.googleapis.com/gcm/send';
$fields = array('registration_ids' => $ids,'data'=> array( "message" => $message ));
$headers = array('Authorization: key=' . $api_key,'Content-Type: application/json');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header'=> $headers ,
'method' => 'POST',
'content' => json_encode($fields),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
?>

I tried to call a post request using PHP code, but I got 401 Unauthorized error.

I tried to send a post request using this PHP code but throw me 401 Unauthorized error:
$username = 'MyDomain\testuser';
$password = '123456';
$url = 'http://10.20.30.40:8080/TargetPage.aspx';
$data = array(
'username' => $username,
'password' => $password,
'postdata' => 'InputParameter1=Test1&InputParameter2=Test2'
);
$options = array(
'http' => array
(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
You're not doing proper basic auth. You have to build the authenticate header yourself:
$options = array(
'http' => array(
'header' => 'Authorization: Basic ' . base64_encode("$username:$password")
)
);
You can't just shove a username/password elements into the array and expect it to work. PHP doesn't know you're building a stream to HTTP basic auth... you have to provide EVERYTHING yourself.

Categories