PHP JSON GCM request from PHP - php

For days I tried to implement a number of solutions from the gcm & php thread here to get my server to send a message to the GCM server and then to my Android device. My call to curl_exec($ch) kept returning false. After a few days of racking my brain, reading and searching the web it seems I finally figured it out. I had to use GET instead of POST, I found that here, and I had to NOT verify the SSL. (I can't remember where I found that...)
I hope this helps someone who's having the same problem. And please, if anyone can improve upon this their corrections are more then welcome.
This is what was suggested by the thread linked to above:
$ch = curl_init();
// WRITE MESSAGE TO SEND TO JSON OBJECT
$message = '{"data":{"message":"here is the message","title":"message title"},"registration_ids":["'. $reg . '","'. $reg2 . '"]}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
curl_setopt($ch, CURLOPT_URL, $gcmurl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// WRITE JSON HEADERS
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization:key=' . $apiKey)
);
$result = curl_exec($ch);
if(!$result > 0){
echo "<p>An error has occurred.<p>";
echo "<p>ERROR: $curl_error</p>";
}
else{
$json_return = json_decode($result);
echo var_dump($json_return);
$info = curl_getinfo($ch);;
echo "<p>httpcode: " . $info['http_code'] . "</p>";
}
curl_close($ch);
For me to get this working I have to implement the following.
$ch = curl_init();
$message = '{"data":{"message":"here is the message","title":"message title"},"registration_ids":["'. $reg . '","'. $reg2 . '"]}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
curl_setopt($ch, CURLOPT_URL, $gcmurl);
/*
* COMMENT OUT THE FOLLOWING LINE
*/
*//curl_setopt($ch, CURLOPT_POST, true);*
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// WRITE JSON HEADERS
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization:key=' . $apiKey)
);
/*
* ADD THESE 2 LINES
*/
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
if(!$result > 0){
echo "<p>An error has occurred.<p>";
echo "<p>ERROR: $curl_error";
}
else{
$json_return = json_decode($result);
echo var_dump($json_return);
$info = curl_getinfo($ch);;
}
curl_close($ch);

$headers = array(
'Accept: application/json',
'Content-Type: application/json',
);
$data = json_encode($input);//Your json data...
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
switch($method)
{
case 'GET':
curl_setopt($handle,CURLOPT_HTTPGET,true);
break;
case 'POST':
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
break;
case 'PUT':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
break;
case 'DELETE':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
case 'FILE':
curl_setopt($handle, CURLOPT_HEADER, 0);
curl_setopt($handle, CURLOPT_VERBOSE, 1);
curl_setopt($handle, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($handle, CURLOPT_POST, true);
echo $data;
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
break;
}
$response = curl_exec($handle);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
return $response;

Have you uncommentted ;extension=php_curl.dll in your php.ini(remote or localhost)

This worked for me cause it returns :
{"multicast_id":8298406095978893272,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1439084688510033%9bd2607ff9fd7ecd"}]}
but I can't receive the notification on the client side.
<?php
class GCM {
//put your code here
// constructor
function __construct() {
}
/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $message) {
// include config
require_once __DIR__ . '/db_config.php';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
// $fields = array(
// 'registration_ids' => $registatoin_ids,
// 'data' => $message,
// );
$fields = '{"to":"caFPICUdKKM:APA91bEJW2vtQHy5IQcOM88WlT5zazf-D9LExvOaSGgAOHqSkHBeHWP34KV1BEKLA9n932BrZXTCwJajug4kcX2LrURY1NJPb9V-vmis1Ra8bo2Zw2BgIIXrfoDbM42Ip6RN_ic1C6eU","data":{"title":"Testing","body":"Success","icon":"R.mipmap.ic_launcher"}}';
$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));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
}
?>

Related

curl request to localhost takes a huge amount of time

I need to make curl request via PHP to the same local virtualhost url. Its really simple. If I make it via Postman or in command line it takes miliseconds. But as I run it in PHP it is still loading without response. I make a log to file after each step so I can say the code stops after Debugger::log(5555555555555).
This is the code:
public function sendRequest(string $url, array $data = null, $method = 'GET')
{
$url = $this->apiUrl . $url;
$ch = curl_init($url);
$json = json_encode($data);
Debugger::log(2222222222222);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
Debugger::log(2222222222222);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
if ($method == 'POST' or $method == 'PUT') {
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
}
Debugger::log(33333333333);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_PORT , 80);
Debugger::log(44444444444);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $this->apiKey,
//'X-API-Token: v4yWSYY2Od7-iA6Vi3e8fRNWMkvii-eF',
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: ' . strlen($json)
]);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
Debugger::log(5555555555555);
$output = curl_exec($ch);
Debugger::log(6666666666666);
if( curl_errno($ch) ) {
Debugger::log(curl_error($ch), Debugger::EXCEPTION);
throw new \Exception( curl_error($ch) );
}
Debugger::log(77777777777777);
$header = curl_getinfo($ch);
Debugger::log(88888888888888);
curl_close($ch);
$return = array("header" => $header, "output" => $output);
return $return;
}

PHP: curl script fails

The task seemed simple.
The login (email) used for authentication must be passed in the login request parameter. In the request body (Body), the user password is passed as a string encoded in UTF-8.
Example request:
POST /auth/authenticate-by-pass?login=testlogin#testDomain.net HTTP/1.1
Host: somehost.ru
Body: somepassword
Cache-Control: no-cache
If the request is successful, the response will contain a JSON object
Tried to do like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://somehost.ru/auth/authenticate-by-pass?login=mylogin#mydomain.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, "mypassword" );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain', 'Host: somehost.ru'));
$result=curl_exec($ch);
$php_obj = json_decode($result, true);
print_r($php_obj);
No result. Nothing is displayed. Please help.
In my understanding what you need can be simply (please amend the headers to further suit your needs if necessary) :
$ch = curl_init();
$post = [
'password' => 'xxxxxx'
];
curl_setopt($ch, CURLOPT_URL, 'http://somehost.ru/auth/authenticate-by-pass?login=mylogin#mydomain.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$php_obj = json_decode($result, true);
print_r($php_obj);
curl_close($ch);
Solved the problem. Ken Lee and everyone else thanks for the help.
$ch = curl_init();
$post = 'mypassword';
curl_setopt($ch, CURLOPT_URL, 'http://somehost.ru/auth/authenticate-by-pass?login=mylogin#mydomain.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$php_obj = json_decode($result, true);
print_r($php_obj);
curl_close($ch);

Connecting to VCenter with PHP using REST API authentication error

I followed the instructions in the official vsphere site to get info from the server and from the answer of another user here .
From what I have understood, firstly I have to get the session id (cis id), but I got "null" as a result.
I tried multiple codes but the result is the same:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = array(
'Content-Type: application/json',
'Accept: application/json',
'vmware-use-header-authn: test'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/com/vmware/cis/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'administrator#vs.dom:userpassword');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
$out = json_decode(curl_exec($ch));
// var_dump($out);
if ($out === false) {
echo 'Curl Error: ' . curl_error($ch);
exit;
}
$sid = $out;
dd($out);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/vcenter/vm");
$output = curl_exec($ch);
$vms = json_decode($output);
var_dump($vms);
curl_close($ch);
The result is null.
this is a script word 100% using PHP
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://{ip}/rest/com/vmware/cis/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'username' . ":" . 'password');
$out = json_decode(curl_exec($ch));
// var_dump($out);
if ($out === false) {
echo 'Curl Error: ' . curl_error($ch);
exit;
}
$sid = $out->value;
curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, "https://{ip}/rest/vcenter/vm");
$output = curl_exec($ch);
$vms = json_decode($output);
var_dump($vms);
curl_close($ch);
?>
when you debug your code, the $out variable return null?
If yes, can you check if your curl returns 56 - OpenSSL SSL_read: Success error in $out = json_decode(curl_exec($ch));?
This error occoured with my code, and I see wich cURL in 7.67 version cause this trouble.
My solution was to downgrade cURL version to 7.65.
It works for me!
I had the same problem, and eventually understood that it is caused by this header:
curl_setopt($ch, CURLOPT_POST, true);
And replacing it with this one solves the issue:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

Translating a cURL request to PHP

The cURL request I am trying to translate is similar to this:
curl -0 -XPOST -u user:pass --data '{"jsonrpc":"2.0","method":"retrieve_summary_info","params":[true, 10],"id":1}' http://127.0.0.1:3141/v2/owner
I want to send this with php and then display the json response but am not sure how I would translate that to PHP
Try this code:
$ch = curl_init();
$data = "{\"jsonrpc\":\"2.0\",\"method\":\"retrieve_summary_info\",\"params\":[true, 10],\"id\":1}";
$user = ''; // set your user
$pass = ''; // set your password
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:3141/v2/owner');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "{$user}:{$pass}");
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
Check this website, it will help you
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:3141/v2/owner');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"jsonrpc\":\"2.0\",\"method\":\"retrieve_summary_info\",\"params\":[true, 10],\"id\":1}");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'user' . ':' . 'pass');
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);

Zoho API-V2 Add Attactmetn URL

I am trying to add attachment url in crm. I am flowing this documentation . But i got error !
This is my code :
$zoho_url = "https://www.zohoapis.com/crm/v2/$module/$id/Attachments";
$post['attachmentUrl'] = $url;
$ch=curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_URL,$zoho_url);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
$headers = array();
$headers[] = "Authorization: ".$authtoken;
$headers[] = "Content-Type: multipart/form-data";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$err = curl_errno($ch);
curl_close ($ch);
if ($err) {
$result = $err;
} else {
$result = $response;
}
print_r($result);
This is response :
{"code":"INVALID_REQUEST","details":{},"message":"unable to process your request. please verify whether you have entered proper method name, parameter and parameter values.","status":"error"}
I made this code for attach a file in a zoho record.
//Get the oauth Token
$accessToken=getCurrentAccessToken();
// files to upload
$tmpfile;
$filename;
$type;
foreach($files as $file){
$tmpfile = $file['tmp_name'];
$filename = basename($file['name']);
$type = $file['type'];
}
$cfile = new CURLFile(realpath($tmpfile),$type,$filename);
$post_data = array (
'file' => $cfile
);
$url = "https://www.zohoapis.com/crm/v2/$module/$id/Attachments";
$headers = array(
'Content-Type: multipart/form-data',
sprintf('Authorization: Zoho-oauthtoken %s', $accessToken)
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$response = curl_exec($ch);
return $response;

Categories