SMS API issue with CURL - php

Hi guys Please someone to help me fix an issue with an sms API. Here is the entire code given by the sms API provider:
I don't know what is wrong and I don't know how to fix the 415 issue. The API provider doens't give coding support
/*
*:: How to generate Token in PHP using our Restful API
*/
"https://restapi.bulksmsonline.com/rest/api/v1/sms/gettoken/username/MYUSERNAME/password/MYPASSWORD",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
/*
*::: How to send sms with your generated Token
*/
"https://restapi.bulksmsonline.com/rest/api/sms/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"from\":\"Sender Name\",\"to\":[\"16813000014\",\"16813000014\"],\"type\":\"Text\",\"content\":\"Sample SMS Content To Be Sent\"}",
CURLOPT_HTTPHEADER => array(
"token : USER TOKEN GENERATED FROM GET TOKEN API",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
And here is the code I developped:
$curl=curl_init("https://restapi.bulksmsonline.com/rest/api/v1/sms/gettoken/username/xxxxx/password/xxxxx");
curl_setopt_array($curl,[
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
]);
$response = curl_exec($curl);
$err = curl_error($curl);
$ch = curl_init("https://restapi.bulksmsonline.com/rest/api/v1/sms/send");
$field = array('from'=>'From me', 'to'=>'237668426982', 'type'=>'Text', 'content'=>'message');
$head = array('token'=>'$response', 'content-type'=>'application/json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING,"" );
curl_setopt($ch, CURLOPT_MAXREDIRS,10 );
curl_setopt($ch, CURLOPT_TIMEOUT,30 );
curl_setopt($ch, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST' );
curl_setopt($ch, CURLOPT_POSTFIELDS,$field );
curl_setopt($ch, CURLOPT_HTTPHEADER,$head );
$output = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $output;
}
Here is the response displayed:
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"|54541add-41130078fe10574f."}
Please help me to fix that.
Thks

Change your post fields and headers as per the API provider's sample. The post fields should be JSON string, not an array. The headers array is not an associative array, it is an array of strings.
$headers = ["token: {$response}", "content-type: application/json"];
$fields = ['from' => 'From me', 'to' => ['237668426982'], 'type' => 'Text', 'content' => 'message'];
$postFields = json_encode($fields);
And then use them for the cURL request,
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
On another note, in your code, you have this 'token'=>'$response', a PHP variable inside single quotes; this will not work. If the variable has a string literal, then you do not need the quotes at all. If you have to use quotes, then use variables inside double quotes.
I hope this helps.

Related

How can I send whatsapp message to multiple numbers by using ultramsg and PHP

I am trying to send WhatsApp message to multiple numbers by using Ultramsg.com using PHP
by reference: https://docs.ultramsg.com/api/post/messages/chat
the code is like this:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.ultramsg.com/instance1xxx/messages/chat",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "token=exxxxxxxx&to=1408XXXXXX1 #c.us,1408XXXXXX2 #c.us&body=WhatsApp API on UltraMsg.com works good&priority=10",
CURLOPT_HTTPHEADER => array(
"content-type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
but I am getting this error
"to": "Wrong 'to' format for element (1408xxxxxxx #c.us,1408xxxxxxx #c.us). 'to' format : 14155552671#c.us or 14155552671-441234567890#g.us"
I am not understanding this.
I think should try to remove space before #c.us

Having an issue with cURL (PHP)

So I have a licensing system that has an API with it, though I barely understand cURL. Could someone please explain what I did wrong?
Most of this code was pulled off of the documentation for the API I'm using.
Here is the code within my PHP login page.
function checkLicense() {
$curl = curl_init();
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.scriptic.xyz/wp-json/lmfwc/v2/licenses/", $licensekey ,"?consumer_key=", $consumer_key ,"&consumer_secret=", $consumer_secret ,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return "cURL Error #: " . $err;
} else {
return $response;
}
}
Error:
cURL Error #: Could not resolve proxy: ?consumer_key=
Edit: When I visit the link using a license key from the database in the spot "$licensekey" and filling in the other two variables it returns the json I want.
The URL it should look like to get the json:
https://www.scriptic.xyz/wp-json/lmfwc/v2/licenses/license-key-example?consumer_key=key-example&consumer_secret=secret-example
Here is the json it returns
{"success":true,"data":{"id":"5","orderId":null,"productId":null,"licenseKey":"hidden-for-privacy","expiresAt":null,"validFor":"0","source":"2","status":"3","timesActivated":null,"timesActivatedMax":"0","createdAt":"2019-11-07 01:43:16","createdBy":"1","updatedAt":null,"updatedBy":null}}
Edit 2:
I have changed , to .
$consumer_key = "hidden-for-privacy";
$consumer_secret = "hidden-for-privacy";
$licensekey = $_SESSION['key'];
function checkLicense() {
$curl = curl_init();
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.scriptic.xyz/wp-json/lmfwc/v2/licenses/". $licensekey ."?consumer_key=". $consumer_key ."&consumer_secret=". $consumer_secret,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return "cURL Error #:" . $err;
} else {
return $response;
}
}
New Error:
{"code":"lmfwc_rest_authentication_error","message":"Consumer key or secret is missing.","data":{"status":401}}
Ok the issue is solved thank you to everyone who helped. I moved the variables inside the function and now it's returning what I want.

PHP Request for Grafana API Returning Error

I am making a request for api chart to be automatically redirected, without password, after redirection, receive the following alert:
SCRIPT
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "3000",
CURLOPT_URL => "http://localhost:3000/d/BSFmuV5Zz/dash?orgId=1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer api-key-here"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "CURL Error #:" . $err; ?>
<?php } else {
echo $response;
}
?>
Operating System: Windows
Download Link: https://grafana.com/grafana/download?platform=windows

PHP CURL give me 401 error but work fine on POSTMAN

I am calling two API one login and second one for add user. To add user first i need to call login API then add user API. when i test these API in postman it work fine but when i call using CURL for add user i am getting 401 error.
Login API in PostMan
Login API
Add User API in POSTMAN
User API
login API PHP code which work fine
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com/sapi/login?action=login",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "login=login&password=password",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
but when i call add user api with the following code it give me error.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com/sapi/profile/generic?action=add",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>
'{"data":{"user":{"generic":{"userid":"Hagrid","password":"abc123","firstname":"afds","lastname":"asf","useremail":"test#gmail.com"}}}}',
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
add the below code before curl_exec
curl_setopt($curl, CURLOPT_COOKIEJAR, "COOKIE.txt");
curl_setopt($curl, CURLOPT_COOKIEFILE, "COOKIE.txt");

IP long string filter

Hey,
i am trying to only show 1 bit of the long string i get from the api i got fram postman, the only thing i need to show is the city. How do i need do this?
i'm trying to find a way with php but i have no clue what to do
a:14:{s:10:"regionName";s:10:"California";s:6:"status";s:7:"success";s:4:"city";s:13:"Mountain View";s:8:"timezone";s:19:"America/Los_Angeles";s:7:"country";s:13:"United States";s:11:"countryCode";s:2:"US";s:3:"zip";s:0:"";s:3:"lon";d:-122.08499908447266;s:3:"isp";s:6:"Google";s:2:"as";s:19:"AS15169 Google Inc.";s:5:"query";s:7:"8.8.8.8";s:6:"region";s:2:"CA";s:3:"lat";d:37.42290115356445;s:3:"org";s:6:"Google";}
(im using the ip of google just for this question)
so the length of the city name changes!
the site where i got it frm http://ip-api.com/php/8.8.8.8
and the code i am using:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ip-api.com/php/8.8.8.8",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: 2e83e542-a6fb-5bb6-94e0-c1908282a2a2"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
That's a product of running a PHP variable in a PHP serialize you can reverse it with unserialize
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ip-api.com/php/8.8.8.8",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: 2e83e542-a6fb-5bb6-94e0-c1908282a2a2"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
$responseArray = unserialize($response); //You probably need some error trapping here
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $responseArray["country"];
}

Categories