posting data in php url as json - php

public function create_plan_paystack(){
$data = array(
"name"=> "Monthly retainer", "interval"=>"monthly", "amount"=>500,'currency'=>'NGN','description'=>'test'
);
$sendjson = array();
$opts = array(
'http'=>array(
'header'=> ["Authorization: Bearer ".$this->config->item('paystack_sk')."\r\n" ,"Content-Type: application/json\r\n"] ,
'method' => 'GET',
'content' => json_encode($data)// http_build_query($data),
)
);
print_r($opts);
$context = stream_context_create($opts);
$json = file_get_contents("https://api.paystack.co/plan/".'PLN_lp20ulqhzezihrc',false,$context);
$obj = json_decode($json);
print_r($obj);
}
I want to post $data array as json .
"https://developers.paystack.co/docs/create-plan" this is the way that they given .plaese help me.

Use curl
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('http://api.local/rest/users');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
Or you can use either of these
https://github.com/yabacon/paystack-php
https://github.com/MalikAbiola/paystack-php-lib

Related

Adding OneSignal segments to filters array

I am using following PHP script to send OneSignal push notifications to subscribed devices.
<?PHP
function sendMessage(){
$content = array(
"en" => 'Testing Message desde el backend small icon '
);
$fields = array(
'app_id' => "xxxx",
'filters' => array(array("field" => "tag", "key" => "correo", "relation" => "=", "value" => "xxx")),
'data' => array("foo" => "bar"),
'small_icon' =>"ic_push",
'contents' => $content
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic xxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);
print("\n\nJSON received:\n");
print($return);
print("\n");
?>
This script is working fine, and the notifications are sent.
Now I need to add more filters, for example I need to add a filter to two other segments that I have created at the OneSignal dashboard.
The segments are "skateboard" and "administrators".
How can I add these two segment to the filters array?
You need to pass segments name array in fields like you are passing tags.
$content = array(
"en" => 'Notification Message Here..'
);
$heading = array(
"en" => 'Heading goes here..'
);
$fields = array(
'app_id' => 'XXXXXXXXXXXXXXXXXXXXX',
'contents' => $content,
'headings' => $heading,
'included_segments' => array('SegmentName1','SegmentName2'),
'tags' = array(array("key" => "state", "relation" => "=", "value" => 'Delhi'))
);
$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic XXXXXXXXXXXXXXXXXXX'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;

Php Variable is null when i pass it to the array

I am trying to send user-specific web notifications with onesignal and i solved everything except this variable null problem.
I use that variable multiple times and there is no problem except these lines:
<?php
if(isset($_POST["sub"]))
{
$my_variable= $_POST["t1"];
$SQL = "some sql here";
if (mysqli_query($db, $SQL)) {
echo $my_variable;
echo "<br>";
function sendMessage(){
$content = array(
"en" => 'test message'
);
$fields = array(
'app_id' => "5b0eacfc-3ac8-4dc6-891b-xxxxx",
'filters' => array(array("field" => "tag", "key" => "key", "relation" => "=", "value" => "$my_variable")),
'data' => array("foo" => "bar"),
'contents' => $content
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Authorization: Basic xxxxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);
The result is :
1 JSON sent: {"app_id":"5b0eacfc-3ac8-4dc6-891b-xxxxx","filters":[{"field":"tag","key":"key","relation":"=","value":"null"}],"data":{"foo":"bar"},"contents":{"en":"test message"}}
I tried so many variations with/without quotas , json_encode() function but couldn't pass that variable to that array.
Your variable is out of scope.
You define $my_variable outside of the function sendMessage(), but proceed to try and use it within the function, without passing it as a parameter.
This can be fixed with the following:
function sendMessage($filterValue)
{
$content = array(
"en" => 'test message'
);
$fields = array(
'app_id' => "5b0eacfc-3ac8-4dc6-891b-xxxxx",
'filters' => array(array("field" => "tag", "key" => "key", "relation" => "=", "value" => $filterValue)),
'data' => array("foo" => "bar"),
'contents' => $content
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Authorization: Basic xxxxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage($my_variable);
$return["allresponses"] = $response;
$return = json_encode( $return);

Search Relevancy Scorer API

I am running this code with a valid API KEY but its showing error-message e.g.
string(179) "{"error":{"code":"Unauthorized","message":"Request is unauthorized to access resource.","details":[{"code":"ScoreRequestUnauthorized","message":"Invalid credentials provided."}]}}"
I am using same API Key in R language and its working fine. It may be a reason that HEADER params are not in correct manner.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$url = 'https://ussouthcentral.services.azureml.net/workspaces/d90e4daf20ce4d28a03a802fcd423f88/services/21c5bf104ffc4528932603b5e71fbc9f/execute?api-version=2.0&details=true';
$data = array(
'Inputs'=> array(
'input1'=> array(
'ColumnNames' => array("query", "p1", "p2", "p3", "p4", "p5"),
'Values' => array( array("value1" , "value2" , "value3"),array("bags", "bags", "bags", "bags"))
)
),
'GlobalParameters'=> null
);
$body = json_encode($data);
$api_key = 'API-KEY';
$headers = array('Content-Type: application/json', 'Authorization: Bearer '.$api_key, 'Accept: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
?>
This is correct code
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$url = 'https://ussouthcentral.services.azureml.net/workspaces/d90e4daf20ce4d28a03a802fcd423f88/services/21c5bf104ffc4528932603b5e71fbc9f/execute?api-version=2.0&details=true';
$data = array(
'Inputs'=> array(
'input1'=> array(
'ColumnNames' => array("query", "p1", "p2", "p3", "p4", "p5"),
'Values' => array( array("bags", "bags", "bags", "bags", "bags", "bags"),array("bags", "bags", "bags", "bags", "bags", "bags"))
)
),
'GlobalParameters'=> null
);
$body = json_encode($data);
$api_key = 'API-KEY';
$headers = array('Content-Type: application/json', 'Authorization: Bearer '.$api_key, 'Accept: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
?>
This is correct code
$url = 'https://ussouthcentral.services.azureml.net/workspaces/d90e4daf20ce4d28a03a802fcd423f88/services/21c5bf104ffc4528932603b5e71fbc9f/execute?api-version=2.0&details=true';
$data = array(
'Inputs'=> array(
'input1'=> array(
'ColumnNames' => array("query", "p1", "p2", "p3", "p4", "p5"),
'Values' => array( array("bags", "bags", "bags", "bags", "bags", "bags"),array("bags", "bags", "bags", "bags", "bags", "bags"))
)
),
'GlobalParameters'=> null
);
$body = json_encode($data);
$api_key = 'API-KEY';
$headers = array('Content-Type: application/json', 'Authorization: Bearer '.$api_key, 'Accept: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
?>

Unable to insert post in blogger using PHP

I am using the following code to insert a post in blogger but the response I am receiving from Google is null. What am I doing wrong?
<?php
$key = "MyKey";
$blog_id = "123456";
$url = 'https://www.googleapis.com/blogger/v3/blogs/'.$blog_id.'/posts/';
$postData = array(
'kind' => 'blogger#post',
'blog' => array('id' => $blog_id),
'title' => 'This is title',
'content' => 'This is content'
);
$data_string = json_encode($postData);
$head = array();
$head[] = 'Authorization: '.$key;
$head[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$rsp = curl_exec($ch);
$results = json_decode($rsp);
var_dump($results);
?>

Add more variables in the URL via GET

I want to do have the possibility to pass parameters through the URL example.
www.example.com/index.php?msg=textOne&var2=textTwo&var3=textThree.
what do I need to change the code to make it work?
<?php
define("GOOGLE_API_KEY", ...);
define("GOOGLE_GCM_URL", "https://android.googleapis.com/gcm/send");
function send_gcm_notify($reg_id, $message, $text) {
$fields = array(
'registration_ids' => array( $reg_id ),
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, GOOGLE_GCM_URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Problem occurred: ' . curl_error($ch));
}
curl_close($ch);
echo $result;
}
$reg_id = "APA91bHuSGES.....nn5pWrrSz0dV63pg";
$msg = filter_input (INPUT_GET, 'msg', FILTER_SANITIZE_STRING);
send_gcm_notify($reg_id, $msg,put);
If you want to use the array style:
....
$getParams = array(
'param1' => 'value',
'param2' => 'value2',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, GOOGLE_GCM_URL . '?' . implode('&', array_map('urlencode', $getParams)));
curl_setopt($ch, CURLOPT_POST, true);
....
Notice the usage of urlencode to insure your parameters are URL safe.

Categories