POST JSON MULTIDIMENSIONAL PHP not working - php

I have try to create post curl json array multidimensional but the result is NULL
i dont know why is not working.
this is my code
$d = array('code' => 1111, 'number' => "08813812");
$array = array('content' => $d);
$result = json_encode($d);
// echo $result;
$data = array(
'content' => $d,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/receive-callback.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"cache-control: no-cache",
"postman-token: 701ab5b6-002e-a982-0cfd-c6eee34ae7e1",
"Content-Type: application/json"
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
echo $response;
this is for receive the post
$d = $_POST['content'];
// file_put_contents('hehe.txt', $d);
print(json_encode($d, JSON_PRETTY_PRINT));
// $ho = json_decode($d);
// echo $d['code'];
in this case , i want the result in receiver is value from object array content

Change your code with the following:-
$d = array('code' => 1111, 'number' => "08813812");
$array = array('content' => $d);
$result = json_encode($d);
// echo $result;
$data = array(
'content' => $d
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/stackoverflow/receive-callback.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"cache-control: no-cache",
"postman-token: 701ab5b6-002e-a982-0cfd-c6eee34ae7e1",
"Content-Type: application/json"
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
echo $response;
To receive use the follwoing :-
$data = json_decode(file_get_contents('php://input'), true);
print_r($data);

Related

PHP post data is not working with AWS4 Signature method for authentication

in the below example, when i am enabling post data curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
getting 'The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method', but when i disabled the post data, its working.
Should i pass this data parameters anywhere else?
$get_token = new GetToken();
$token_obj = $get_token->get_session_token();
$accessKeyID = $token_obj->access_key_id;
$secretAccessKey = $token_obj->secret_key;
$regionName = $token_obj->region;
$serviceName = 'execute-api';
$httpMethodName = 'POST';
$canonicalURI = '/orders/salesorder';
$queryParametes = array();
$awsHeaders = array('content-type'=>'application/json','host'=>'api.mysite.com','id_token'=>$token_obj->id_token,'x-amz-date'=>gmdate("Ymd\THis\Z"),'x-amz-security-token'=>$token_obj->session_token);
$payload = "";
$get_aws4_sign = new AWSV4($accessKeyID,$secretAccessKey,$regionName,$serviceName,$httpMethodName,$canonicalURI,$queryParametes,$awsHeaders,$payload);
$headers_result = $get_aws4_sign->getHeaders();
$headersArr = array(
'host' => $headers_result['host'],
'id_token' => $headers_result['id_token'],
'xamzdate' => $headers_result['x-amz-date'],
'xamzsecuritytoken' => $headers_result['x-amz-security-token'],
'Authorization' => $headers_result['Authorization']
);
$data = array("id" => "126757879");
$data_string = json_encode($data);
$ch = curl_init('https://api.mysite.com/orders/salesorder');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'content-type: '.$headers_result['content-type'],
'host: '.$headers_result['host'],
'id_token: '.$headers_result['id_token'],
'x-amz-date: '.$headers_result['x-amz-date'],
'x-amz-security-token: '.$headers_result['x-amz-security-token'],
'Authorization: '.$headers_result['Authorization']
));
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//execute post
$result = json_decode( curl_exec($ch), TRUE);
//close connection
curl_close($ch);
print_r($result);
$get_token = new GetToken();
$token_obj = $get_token->get_session_token();
$accessKeyID = $token_obj->access_key_id;
$secretAccessKey = $token_obj->secret_key;
$regionName = $token_obj->region;
$serviceName = 'execute-api';
$httpMethodName = 'POST';
$canonicalURI = '/orders/salesorder';
$queryParametes = array();
$awsHeaders = array('content-type'=>'application/json','host'=>'api.mysite.com','id_token'=>$token_obj->id_token,'x-amz-date'=>gmdate("Ymd\THis\Z"),'x-amz-security-token'=>$token_obj->session_token);
$data = array("id" => "126757879");
$data_string = json_encode($data);
$payload = $data_string;
$get_aws4_sign = new AWSV4($accessKeyID,$secretAccessKey,$regionName,$serviceName,$httpMethodName,$canonicalURI,$queryParametes,$awsHeaders,$payload);
$headers_result = $get_aws4_sign->getHeaders();
$headersArr = array(
'host' => $headers_result['host'],
'id_token' => $headers_result['id_token'],
'xamzdate' => $headers_result['x-amz-date'],
'xamzsecuritytoken' => $headers_result['x-amz-security-token'],
'Authorization' => $headers_result['Authorization']
);
$ch = curl_init('https://api.mysite.com/orders/salesorder');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'content-type: '.$headers_result['content-type'],
'host: '.$headers_result['host'],
'id_token: '.$headers_result['id_token'],
'x-amz-date: '.$headers_result['x-amz-date'],
'x-amz-security-token: '.$headers_result['x-amz-security-token'],
'Authorization: '.$headers_result['Authorization']
));
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//execute post
$result = json_decode( curl_exec($ch), TRUE);
//close connection
curl_close($ch);
print_r($result);

How send POST request with JSON in php?

I try to send request from post type to server but it not success, It return me BadRequest with status 400,
My code:
<?php
$products = array('1'=>array('amount'=>'1','product_id'=>'11250'));
$data = array('id' => '67', 'shipping' => '61', 'payment'=> '2','products'=> $products);
$cert = base64_encode('myapp#myapp.co.il:1234abcd');
$url = "https://myapp.co.il/api/aaa";
$ch = curl_init($url);
$payload = json_encode($data);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"authorization: Basic ".$cert,
"Content-Type: application/json",
"cache-control: no-cache"
));
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
?>
It return me so:
array (2) {["message"] => string (51) "Bad Request: Syntax error, distorted JSON" ["status"] => int (400)}
I see that you prepared variable $payload to send, but sent $postdata instead at curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata).

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

Firebase FCM error JSON_PARSING_ERROR: Unexpected token END OF FILE at position

I am trying to send notifications through firebase's fcm service using php. Here is what I got so far:
$ch = curl_init();
$payload = [
'to' => '/topics/'.ANDROID_TOPIC,
'notification' => [
'message' => 1
]
];
$headers = [
'Content-Type: application/json',
'Content-length: '.sizeof(json_encode($payload)),
'Authorization: key='.FIREBASE_KEY
];
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
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($payload));
$result = curl_exec($ch );
curl_close($ch);
return $result;
However, I am getting Unexpected token END OF FILE at position 2 response from firebase.
What do you think is the reason this is happening?
Remove Content-length line:
$header = array();
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: key=' . FIREBASE_KEY;
$payload = [
'to' => 'verybigpushtoken',
'notification' => [
'title' => "Portugal VS Germany",
'body' => "1 to 2"
]
];
$crl = curl_init();
curl_setopt($crl, CURLOPT_HTTPHEADER, $header);
curl_setopt($crl, CURLOPT_POST,true);
curl_setopt($crl, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($crl, CURLOPT_POSTFIELDS, json_encode( $payload ) );
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true );
// curl_setopt($crl, CURLOPT_SSL_VERIFYHOST, false); should be off on production
// curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, false); shoule be off on production
$rest = curl_exec($crl);
if ($rest === false) {
return curl_error($crl)
}
curl_close($crl);
return $rest;

Sending Push Notifications via OneSignal using PHP

I am using the following PHP Code to send push notification via One Signal:
function sendMessage(){
$content = array(
"en" => 'English Message'
);
$fields = array(
'app_id' => $appId,
'included_segments' => array('All'),
'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 '.$restKey));
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");
And I am getting the following response:
JSON sent: {"app_id":null,"included_segments":["All"],"data":{"foo":"bar"},"contents":{"en":"English Message"}} JSON received: "{\"allresponses\":\"{\\\"adm_big_picture\\\":null,\\\"adm_group\\\":null,\\\"adm_group_message\\\":null,\\\"adm_large_icon\\\":null,\\\"adm_small_icon\\\":null,\\\"adm_sound\\\":null,\\\"amazon_background_data\\\":false,\\\"android_accent_color\\\":null,\\\"android_group\\\":null,\\\"android_group_message\\\":null,\\\"android_led_color\\\":null,\\\"android_sound\\\":null,\\\"android_visibility\\\":null,\\\"app_id\\\":\\\"63c6c8f7-694b-4c68-abc1-d820d9bbbec1\\\",\\\"big_picture\\\":null,\\\"buttons\\\":null,\\\"canceled\\\":false,\\\"chrome_big_picture\\\":null,\\\"chrome_icon\\\":null,\\\"chrome_web_icon\\\":\\\"\\\",\\\"chrome_web_image\\\":\\\"\\\",\\\"content_available\\\":false,\\\"contents\\\":{\\\"en\\\":\\\"This is a new message.\\\"},\\\"converted\\\":0,\\\"data\\\":null,\\\"delayed_option\\\":\\\"immediate\\\",\\\"delivery_time_of_day\\\":\\\"4:00 PM\\\",\\\"errored\\\":0,\\\"excluded_segments\\\":[],\\\"failed\\\":0,\\\"firefox_icon\\\":\\\"\\\",\\\"headings\\\":{\\\"en\\\":\\\"New Message\\\"},\\\"id\\\":\\\"8d56f592-8f43-461a-94e8-2fe9922ba844\\\",\\\"include_player_ids\\\":null,\\\"included_segments\\\":[\\\"All\\\"],\\\"ios_badgeCount\\\":null,\\\"ios_badgeType\\\":null,\\\"ios_category\\\":null,\\\"ios_sound\\\":null,\\\"isAdm\\\":false,\\\"isAndroid\\\":false,\\\"isChrome\\\":false,\\\"isChromeWeb\\\":true,\\\"isFirefox\\\":true,\\\"isIos\\\":false,\\\"isSafari\\\":true,\\\"isWP\\\":false,\\\"isWP_WNS\\\":false,\\\"large_icon\\\":null,\\\"priority\\\":null,\\\"queued_at\\\":1492523636,\\\"remaining\\\":0,\\\"send_after\\\":1492523636,\\\"small_icon\\\":null,\\\"successful\\\":3,\\\"tags\\\":null,\\\"filters\\\":null,\\\"template_id\\\":null,\\\"ttl\\\":null,\\\"url\\\":\\\"\\\",\\\"web_buttons\\\":null,\\\"wp_sound\\\":null,\\\"wp_wns_sound\\\":null}\"}"
However the Push Notification is not appearing in the One Signal Dashboard and neither being received by those who subscribed.
Can someone help please :) ?
your mistake is here.
'Authorization: Basic '.$restKey));
Correct one is.....
'Authorization: Basic "'.$restKey."'"));
I am working this code and this is working fine
function SendOneSignalMessage($message,$empid){
// Your code here!
$fields = array(
'app_id' => 'xxxxxxxxxxxxxxxxxx',
'include_player_ids' => [$empid],
'contents' => array("en" =>$message),
'headings' => array("en"=>"etc"),
'largeIcon' => 'https://cdn4.iconfinder.com/data/icons/iconsimple-logotypes/512/github-512.png',
);
$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 xxxxxxxxxxxxx));
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);
}

Categories