I want to push notification with below code
the json I want to get is something like this
{
"data": {
"message": "Hello! Welcome to parse notifications.",
"title": "AndroidHive"
},
"is_background": false
}
below is php code
<?php
$url = 'https://api.parse.com/1/push';
$APPLICATION_ID = "T7G0qlJG5ygBVHZFVrqf8VO17vhQeeG3TnshxfQy";
$REST_API_KEY = "ywsoZIXPlrapASLHu5kvNBdCl7kfPe43OW3ugyZd";
$url = 'https://api.parse.com/1/push';
$data = array(
'channles'=>'',
'data'=>array('data'=>array(
'message'=>'Hello! Welcome to parse notifications.',
'title'=> 'ParseTest',
),
"is_background"=> false
),
);
$_data = json_encode($data);
$headers = array(
'X-Parse-Application-Id: ' . $APPLICATION_ID,
'X-Parse-REST-API-Key: ' . $REST_API_KEY,
'Content-Type: application/json',
);
$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);
?>
But nothing send to my device, anyone know how to solve it? Thank you.
The problem is the curl,add such code with make the curl work fine.
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER ,false);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST ,false);
Related
I am trying to retrieve data from salesforce using the REST api and CURL in PHP.
I perform the authentication request and recieve an 'instance_url' and 'access_token' but after performing a query request using those, i receive an "INVALID_SESSION_ID" error.
my authentication request code:
function get_sf_auth_data() {
$post_data = array(
'grant_type' => 'password',
'client_id' => 'xxxxxxxxxxxxxxxxxxxxxx', //My client id (xxx... for this example)
'client_secret' => '111111111111', // My client secret (111... for this example)
'username' => 'my_user_name',
'password' => 'my_user_password'
);
$headers = array(
'Content-type' => 'application/x-www-form-urlencoded;charset=UTF-8'
);
$curl = curl_init('https://login.salesforce.com/services/oauth2/token');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$response = curl_exec($curl);
curl_close($curl);
// Retrieve and parse response body
$sf_access_data = json_decode($response, true);
echo '*********' . $sf_response_data['instance_url'] . '********';
echo '*********' . $sf_response_data['access_token'] . '********';
return $sf_access_data;
}
My query request code:
function get_user_sfid($sf_access_data, $user_id_number){
$sql = "SELECT Id, Name
FROM Account
WHERE ID__c = '$user_id_number'";
$url = $sf_access_data['instance_url'] . '/services/data/v20.0/query/?q=' . urlencode($sql);
$headers = array(
'Authorization' => 'OAuth ' . $sf_access_data['access_token']
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$json_response = curl_exec($curl);
curl_close($curl);
var_dump($json_response); // This prints out the response where i got the error
$response = json_decode($json_response);
return $response['id'];
}
The response to the query request as is:
[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]
I have also tried using this guide (http://developer.force.com/cookbook/recipe/interact-with-the-forcecom-rest-api-from-php) as reference but it uses an "authorization_code" authentication and not a password one.
EDIT
The app has full access in salesforce and both api version and authentication data are correct
I found the solution.
I defined my headers array as:
$headers = array(
"Authorization" => "OAuth " . $sf_access_data['access_token']
);
When i should have defined it as:
$headers = array(
"Authorization: OAuth " . $sf_access_data['access_token']
);
I try to send email from a signed Outlook Account with Outlook Rest Api and Curl then I get this error
Request returned status 400
This is my code for sending mail
private static $outlookApiUrl = "https://outlook.office.com/api/v2.0";
public static function sendMail ($access_token,$user_email,$subject,$Content,$email){
$arr= array(
"Message" =>array(
'Subject' => $subject,
"Body"=>array(
"Content-Type"=>"HTML",
"Content"=>$Content,
),
"ToRecipients"=>array(
array(
"EmailAddress"=>array(
"Address"=>$email,
)
),
),
));
$json=json_encode($arr, true);
$getMessagesUrl = self::$outlookApiUrl."/me/sendmail";
return self::makeApiCall($access_token, $user_email, "POST",$getMessageUrl,$json);
}
and this is the code for CURL
public static function makeApiCall($access_token, $user_email, $method, $url, $payload = NULL) {
// Generate the list of headers to always send.
$headers = array(
"User-Agent: php-tutorial/1.0",
"Authorization: Bearer ".$access_token,
"Accept: application/json",
"client-request-id: ".self::makeGuid(),
"return-client-request-id: true",
"X-AnchorMailbox: ".$user_email
);
$curl = curl_init($url);
switch(strtoupper($method)) {
case "POST":
error_log("Doing POST");
$headers[] = "Content-Type: application/json";
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
break;
default:
error_log("INVALID METHOD: ".$method);
exit;
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
error_log("curl_exec done.");
$curl_errno = curl_errno($curl);
$curl_err = curl_error($curl);
if ($curl_errno) {
//PRINT ERROR
}
else {
error_log("Response: ".$response);
curl_close($curl);
return json_decode($response, true);
}
}
THEN I call the sendMail method at Home page
$send=OutlookService::sendMail($_SESSION["access_token"], $_SESSION["user_email"],"testing","<html><body>testing email.</body></html>","example#gmail.com");
echo var_dump($send);
Can I know what's wrong with my code ? and why would I get this error?
The property 'Content-Type' does not exist on type 'Microsoft.OutlookServices.ItemBody' , It should be 'ContentType' . Please refer to this document for details :
https://msdn.microsoft.com/office/office365/api/complex-types-for-mail-contacts-calendar#ItemBody
Also you need to 'send mail as a user' permission for O365 Exchange Online in Azure AD if you want to send a mail message with REST API . You could also refer to below article for more details:
https://dev.outlook.com/restapi/tutorial/php
I'm trying to post json data using cURL to
an API script that submits the data to an
application. I use file_get_contents('php://input')
and the data does not get submitted to the application
But if I type in an actual email address in the "contact_email"
in the API it submits the email to the application.
Here is the cURL script first:
`
$data = '
{
"customer":
{
"first_name":"John",
"last_name":"Smith",
"email":"frednow9901#aol.com",
"phone_number":"2125555555",
"billing_address":"212 Any Street",
"billing_city":"Any City",
"billing_state":"New York",
"billing_zip":"10012",
"billing_country":"USA"
}
}
';
$ch = curl_init('http://example.com/acadd.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
$result = curl_exec($ch);
`
and here is the API script it posts to:
<?php
$json_data = file_get_contents('php://input');
$cart = json_decode( $json_data );
$email = $cart->customer->email .
// Set up an object instance using our PHP API wrapper.
define("AC_URL", "https://account.api-us1.com");
define("AC_API_KEY", "api key");
require_once("./ac-api-php/includes/ac.class.php");
$ac = new AC(AC_URL, AC_API_KEY);
$post_data = array(
"contact_email" => $email , // include this or contact_id
"automation" => "9", // one or more
);
$response = $ac->api("automation/contact/add", $post_data);
echo "<pre>";
print_r($response);
echo "</pre>";?>
Take a look at this line:
$email = $cart->customer->email .
You have a period after the retrieval of the email property. This attempts to concat that with the define statement which would return 1. So sample#email.com would actually be sample#email.com1.
I created a PHP-script which is uploading an image, picked up from an HTML-form, to the Parse.com backend.
Actually it looks like the script is working, because I am getting a URL and a name as result.
After the Upload I am associating the uploaded file with an Object.
The result is also looking fine and the image is appearing inside the data browse.
But if I try to get the image in my iOS app or take a look at it inside of the browser (by clicking on it) I only get an access denied alert or a white page (with broken image icon).
iOS Error: Error Domain=Parse Code=150 "The operation couldn’t be completed. (Parse error 150.)
Here you can see my code:
Upload image:
$teamImage = $_FILES["teamImage"];
$APPLICATION_ID = "XXXXXXXXXXXXXXXXXXX";
$REST_API_KEY = "XXXXXXXXXXXXXXXXXXX";
$urlFile = 'https://api.parse.com/1/files/' . $teamImage['name'];
$image = $teamImage['tmp_name'];
$headerFile = array(
'X-Parse-Application-Id: ' . $APPLICATION_ID,
'X-Parse-REST-API-Key: ' . $REST_API_KEY,
'Content-Type: ' . $teamImage['type'],
'Content-Length: ' . strlen($image),
);
$curlFile = curl_init($urlFile);
curl_setopt($curlFile, CURLOPT_POST, 1);
curl_setopt($curlFile, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlFile, CURLOPT_POSTFIELDS, $image);
curl_setopt($curlFile, CURLOPT_HTTPHEADER, $headerFile);
curl_setopt($curlFile, CURLOPT_SSL_VERIFYPEER, false);
$responseFile = curl_exec($curlFile);
$httpCodeFile = curl_getinfo($curlFile, CURLINFO_HTTP_CODE);
$result = array('code'=>$httpCodeFile, 'response'=>$responseFile);
Associating image to Object (image name for test case hardcoded)
$url = 'https://api.parse.com/1/classes/Teams';
$data = array(
'name' => 'Test',
'teamImage' => array(
'name' => '......jpg',
'__type' => 'File'
),
);
$_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_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$responseTwo = curl_exec($curl);
$httpCodeTwo = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$resultTwo = array('code'=>$httpCodeTwo, 'response'=>$responseTwo);
Image Url, which I am getting back from Parse: http://files.parse.com/725d8f61-de18-4de5-a84c-dcc6e74c43ae/197e7bb6-62ad-4dc4-a011-6db88333ac45-BMW_1series_3door_Wallpaper_1920x1200_01.jpg
Data Browser Screenshot:
I think you need to specify the url as well
$data = array(
'name' => 'Test',
'teamImage' => array(
'name' => '......jpg',
'__type' => 'File',
'url' => '....jpg'
),
);
I am trying to send data to device with GCM but getting curly braces in output(as below image). I'm new to php and I wonder how to fix this issue. Here is the part of PHP server code :
public function send_notification($registatoin_ids, $message, $title) {
// include config
include_once './config.php';
$url = 'https://android.googleapis.com/gcm/send';
$data = array("title" => $title, "message" => $message);
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $data
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
/////////
I changed the send_message.php like this and it fixed.
if (isset($_GET["regId"]) && isset($_GET["message"]) && isset($_GET["title"])) {
$regId = $_GET["regId"];
$messagem = $_GET["message"];
$titlem = $_GET["title"];
include_once './GCM.php';
$gcm = new GCM();
$registatoin_ids = array($regId);
$message = array("message" => $messagem);
$title = array($titlem);
$result = $gcm->send_notification($registatoin_ids, $messagem, $titlem);
echo $result;`
}
EDIT: sorry, with JSON GCM payload data should be an assoc array. Mixed up with an older flavor of the protocol. That said, what is your intent on the Android side like?
EDIT: dump the whole extra bundle.
Bundle b = intent.getExtras();
for(String k : b.keySet())
Log.d("tag", k + "=" + b.get(k).toString());
edited:
ok, I think your problem is you're posting the value {.....} with no key. it's like saying
http://..... com?{myjson:"stuff"}
I haven't reviewed the api, but it seems it should be something like post "data=".json_encode($fields);