Sending push notification using parse using query - php

I want to send push notification using query
for example
I have this query
$ages = array(18,19,20,21)
$parseQuery = new parseQuery('_User');
$parseQuery->whereContainedIn('age', $ages);
$result = $parseQuery->find();
and I want to send them a push notification
Should I use the Installation or is theres another way ?
Thanks
UPDATE:
I am using this sdk
https://github.com/apotropaic/parse.com-php-library
from the code I am returning list of people I am going to send them notification
I want to send the push notifications to the _User class

It looks like you're using parseQuery to retrieve a user with the age constraints you specified. Once you have the user data in your $result, you can use curl to send push notifications, according to the docs.
Here's a bit of code I found on the web, it should be simple to adopt it to your existing code to send a push notification to the user you've retrieved.
<?php
$APPLICATION_ID = "xxxxxxxx";
$REST_API_KEY = "xxxxxxxxx";
$MESSAGE = "your-alert-message";
$url = 'https://api.parse.com/1/push';
$data = array(
'channel' => '',
'type' => 'android',
'expiry' => 1451606400,
'where' => array(
'age' => array('$in' => array(18,19,20,21))
),
'data' => array(
'alert' => 'greetings programs',
),
);
$_data = json_encode($data);
$headers = array(
'X-Parse-Application-Id: ' . $APPLICATION_ID,
'X-Parse-REST-API-Key: ' . $REST_API_KEY,
'Content-Type: application/json',
'Content-Length: ' . strlen($_data),
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_exec($curl);
It's worth noting that you need php-curl for this to work. It's very easy to install or enable if you don't already have it. Alternatively you can use file_get_contents to achieve he same thing if you want to use something out of the box.
Edit: I've updated the $data array with the constraints you defined in the question. You don't need to use parseQuery to find users with the updated code. You're effectively telling Parse to apply the constraints with the call.
Update 2: I haven't tested this code but the library makes it very straightforward how to setup in the README. The library is just using curl underneath so you can use whichever is more convenient.
$parse = new parsePush('Sample push message');
$parse->where = array(
'age' => array('$in' => array(18,19,20,21))
);
//create acl
$parse->ACL = array("*" => array("write" => true, "read" => true));
$r = $parse->send();

Related

GET api for Parse Server with Where clause PHP

I am having the following code to make a GET statement to the REST API of Parse server using PHP:
$query = json_encode(
array(
'where' => array( 'userid' => "8728792347239" )
));
echo $query;
$ch = curl_init('https://*hidden*.herokuapp.com/parse/classes/computers?'.$query);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'X-Parse-Application-Id: *hidden*',
'X-Parse-REST-API-Key: *hidden*',
'Content-Type: application/json'
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
print_r($response);
However I am getting the following error:
{"code":102,"error":"Invalid parameter for query: {\"where\":{\"userid\":\"8728792347239\"}}"}
What am I doing wrong?
without having read the documentation, i bet it's supposed to be url-encoded, not json-encoded -OR- that the data is supposed to be in the POST body, not the URL query.
if guess #1 is correct, then your problem is that you're using json_encode instead of http_build_query eg
$query = http_build_query(
array(
'where' => array( 'userid' => "8728792347239" )
));
if guess #2 is correct, then your problem is that you're adding the data to the url query instead of adding it to the request body, eg
$ch = curl_init('https://*hidden*.herokuapp.com/parse/classes/computers');
curl_setopt($ch,CURLOPT_POSTFIELDS,$query);

My page won't answer the Messenger call

I'm developing some code that answers via Facebook Messenger and I use this piece of code to handle the answer and answer back:
if(isset($_REQUEST['hub_verify_token'])) {
if ($_REQUEST['hub_verify_token'] === A::VERIFY_TOKEN) {
echo $_REQUEST['hub_challenge'];
exit;
}
}
$input = json_decode(file_get_contents('php://input'), TRUE);
$sender_id = $input['entry'][0]['messaging'][0]['sender']['id'];
$message_text = $input['entry'][0]['messaging'][0]['message']['text'];
$fb_handler = new FBHandler($sender_id);
/*$to_send = array(
'recipient' => array('id' => $sender_id ),
'message' => array('text' => "Hi" )
);*/
$to_send = $fb_handler->talk($message_text);
$ch = curl_init('https://graph.facebook.com/v2.6/me/messages?access_token='.A::ACCESS_TOKEN);
curl_setopt_array($ch, array(
CURLOPT_POSTFIELDS => json_encode($to_send),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => array('Content-Type: application/json')
));
$response = curl_exec($ch);
error_log($response['error']);
curl_close($ch);
What I don't understand is why the commented $to_send works but the non-commented not, because the code in the A class is correct, it composes correctly the array.
So I came up with an idea and I'm asking you if it's correct (because I found nothing in the Facebook Developers documentation): is it possible that the array that will be sent to Messenger has to be composed in the same set of instructions?
I know it sounds ridiculous, but I dunno what else to think.
Also I noticed that error_log doesn't work: nothing has been written in the php_errorlog file. What's happening?

How to use authorize.net without framework

I am having problems just getting authorize.net to work. I feel Like I am missing something, but not sure what it is. I follow this guide here on the website: http://developer.authorize.net/hello_world/ I have composer setup correctly I believe, but when I run the sample code I get an error
Fatal error: Class 'Goetas\Xsd\XsdToPhp\Jms\Handler\BaseTypesHandler' not found in /home/admin/web/*****/public_html/pricing/vendor/authorizenet/authorizenet/lib/net/authorize/api/controller/base/ApiOperationBase.php on line 82
Note: The stars in the location after "/web/" were to hide the domain that was listed for security reasons.
Any clue how I can get this to work?
Does anyone have a step by step guide to integrate authorize.net into a custom built platform? We are just trying to send the basic information needed to charge a card and receive a response back.
To anyone running into issues with Trying to quickly integrate authorize.net into their pay system or website. Below is the code I used to built/customized to get this to work. The framework is not needed unless you are creating a fully integrated system, which is not clearly indicated on the site. You are obviously going to have to expand on this code further, but this should be what anyone needs to integrate into authorize quickly.
$params = array(
'x_invoice_num' => 'test',
'x_amount' => '5',
'x_exp_date' => '1202',
'x_address' => 'test',
'x_zip' => '12345',
'x_first_name' => 'test',
'x_last_name' => 'test',
'x_relay_response' => false,
'x_type' => 'AUTH_CAPTURE',
'x_method' => 'CC',
'x_login' => 'yourlogin code goes here',
'x_tran_key' => 'your trans key goes here',
'x_card_num' => '4111111111111111',
'x_card_code' => '143',
'x_delim_data' => true,
'x_delim_char' => '|',
'x_relay_response' => false
);
$postString = '';
foreach ($params as $key => $value)
$postString .= $key.'='.urlencode($value).'&';
$postString = trim($postString, '&');
$url = 'https://secure.authorize.net/gateway/transact.dll';
$request = curl_init($url);
curl_setopt($request, CURLOPT_HEADER, 0);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_POSTFIELDS, $postString);
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($request, CURLOPT_SSL_VERIFYHOST, false);
$postResponse = curl_exec($request);
curl_close($request);
print_r($postResponse);
$response = explode('|', $postResponse);
if (!isset($response[7]) || !isset($response[3]) || !isset($response[9]))
{
$msg = 'Authorize.net returned a malformed response for cart';
if (isset($response[7]))
$msg .= ' '.(int)$response[7];
die('Authorize.net returned a malformed response, aborted.');
}
$message = $response[3];
switch ($response[0]) // Response code
{
case 1: // Payment accepted
print_r($response[1]);
break ;
case 4: // Hold for review
print_r($response[4]);
break ;
default:
echo $message;
exit;
}

Parse.com sending push notification to multiple device types i.e Android and iOS not working

I am using following code to send parse.com push notification form my PHP form using REST API.
$APPLICATION_ID = 'Applicatio_id_here';
$REST_API_KEY = 'Rest_api_key_here';
$data = [
'where' => ['channels' => ''],
'expiry' => time() + 1000,
'data' => [
'alert' => $notification,
],
];
$_data = json_encode($data);
$headers = [
'X-Parse-Application-Id: ' . $APPLICATION_ID,
'X-Parse-REST-API-Key: ' . $REST_API_KEY,
'Content-Type: application/json',
'Content-Length: ' . strlen($_data),
];
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_exec($curl);
My parse.com account is setup for both Android and iOS push notification. And I am able to get notification on Android device successfully. But the problem is with the iPhone. I am not getting any notification on iPhone device.
But iPhone device is receiving notification successfully, if I send the notification from the parse.com admin (Send a push).
One more thing to mention, in parse.com admin, I could see Channel = ["global"] for iOS devices but [""] blank for Android device.
So I think there might be some problem in my PHP code.
If it is not possible to send same message with this code, please help me with some working code. I need this as my app for both devices are already online.
Try to change your "$data" to this one:
$data = [ 'where' => ['channels' => 'global'] OR ['channels' => ''], 'expiry' => time()
Another option :
$data = ['where' => 'channels' => 'global' OR 'channels' => '', 'expiry' => time()

Php: get response from a posted form on an external service

This would seem an easy task, but I can't get it to work.
I need to access some data publicly provided by the Bank of Mexico. The data is available through a form you can find at the link: http://www.banxico.org.mx/SieInternet/consultarDirectorioInternetAction.do?accion=consultarCuadro&idCuadro=CP5&locale=es
You can see an example of the data I need by clicking on the button "html" on the top left section. Once that table is opened I know how to fetch the data I need and work with them. However, I'd like to have this as an automated task so that the script can check regularly when new data is available.
So, I am trying to use file_get_contents() along with stream_context_create() to post the parameters I need and open the result page, so I can work with it.
I tried a few different ways (first I was using http_post_fields() ), but nothing seems to work.
Right now my code is this:
<?php
$url = 'http://www.banxico.org.mx/SieInternet/consultarDirectorioInternetAction.do?accion=consultarSeries';
$data = array(
'anoFinal' => 2015,
'anoInicial' => 2015,
'formatoHTML.x' => 15,
'formatoHTML.y' => 7,
'formatoHorizontal' => false,
'idCuadro' => 'CP5',
'locale' => 'es',
'sector' => 8,
'series' => 'SP1',
'tipoInformacion' => '',
'version' => 2
);
$postdata = http_build_query($data);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
//returns bool(false)
?>
What am I missing? I noticed that the page does in fact return nothing if wrong parameters are sent (as you can see by opening simply http://www.banxico.org.mx/SieInternet/consultarDirectorioInternetAction.do?accion=consultarSeries without any post data: nothing is returned), thus I'm not sure whether the post is successful but nothing is returned because some parameters are wrong, or if the code is wrong.
The posted data should be fine, as I copied them directly from a successfull query I made manually.
What am I missing?
It turns out cURL is a better way to do this, thanks to CBroe for the advice.
Here is the fixed code I am using, if someone else needs it:
<?php
//$url and $data are the same as above
//initialize cURL
$handle = curl_init($url);
//post values
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
//set to return the response
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
//execute
$response = (curl_exec( $handle ));
?>

Categories