php $_REQUEST not working when passing data through android - php

<?php
if(isset($_REQUEST['body']))
{
$body = $_REQUEST['body'];
$api_key = "tHx7zm7eb7z3yKHtNb";
$password = "x";
$yourdomain = "appicmobile";
$ticket_data = json_encode(array(
"body" => $body
));
$url = "https://$yourdomain.freshdesk.com/api/v2/tickets/32/reply";
$ch = curl_init($url);
$header[] = "Content-type: application/json";
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$password");
curl_setopt($ch, CURLOPT_POSTFIELDS, $ticket_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
$info = curl_getinfo($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($server_output, 0, $header_size);
$response = substr($server_output, $header_size);
if($info['http_code'] == 201)
{
// "Ticket created successfully, the response is given below \n";
//echo "Response Headers are \n";
//echo $headers."\n";
//echo "Response Body \n";
echo "$response";
}
else
{
if($info['http_code'] == 404)
{
echo "Error, Please check the end point \n";
}
else
{
echo "Error, HTTP Status Code : " . $info['http_code'] . "\n";
echo "Headers are ".$headers;
echo "Response are ".$response;
}
}
curl_close($ch);
}
else {
echo "error";
}
?>
While entering data through android using php shows empty.I am using volley to pass the data.
I'm trying to pass data from Android to my PHP application, but it seems to POST method is not working well, once all $_REQUEST variables are empty.
Tried all the method get,post but still not working

Related

Create user using freshdesk PHP api

I'm implementing freshdesk API's for one of my project.
Using freshdesk PHP API I tried to create new user but i'm getting internal server error when I run the code in postman plugin and i'm using SLIM framework for my project.
Please check my below code and thanks in advance!
PHP:
<?php
$api_key = "MY_API_KEY";
$password = "xyz123M";
$mydomain = "MY_DOMAIN";
$custom_fields = array(
"department" => "Accounts"
);
$contact_data = json_encode(array(
"user_name" => "Sri Jitthu",
"email_id" => "srij#mydomain.com"
));
$url = "https://$mydomain.freshdesk.com/api/v2/contacts";
$ch = curl_init($url);
$header[] = "Content-type: application/json";
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$password");
curl_setopt($ch, CURLOPT_POSTFIELDS, $contact_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
$info = curl_getinfo($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($server_output, 0, $header_size);
$response = substr($server_output, $header_size);
if($info['http_code'] == 201) {
echo "Contact created successfully, the response is given below \n";
echo "Response Headers are \n";
echo $headers."\n";
echo "Response Body \n";
echo "$response \n";
}
else
{
if($info['http_code'] == 404)
{
echo "Error, Please check the end point \n";
}
else
{
echo "Error, HTTP Status Code : " . $info['http_code'] . "\n";
echo "Headers are ".$headers."\n";
echo "Response is ".$response;
}
}
curl_close($ch);
?>
I have used same freshdesk PHP API. I think the parameters that you have defined is not acceptable by fresh desk.We need to pass the parameters as they defined.
Instead of passing user_name you should pass name also instead of passing email_id you should pass email.
As well as when you're passing custom fields in your API means that should exists or predefined in your account then only it will work. for ex you're passing department is Accounts in your API which means the Accounts should be predefined in your freshdesk account.
Check the below code and try. Hope it will help you.
PHP:
<?php
$api_key = "YOUR_API_KEY";
$password = "xyz";
$yourdomain = "YOUR_DOMAIN";
$custom_fields = array(
"department" => "Accounts"
);
$contact_data = json_encode(array(
"name" => "Sri Jitthu",
"email" => "srij#mydomain.com"
));
$url = "https://$yourdomain.freshdesk.com/api/v2/contacts";
$ch = curl_init($url);
$header[] = "Content-type: application/json";
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$password");
curl_setopt($ch, CURLOPT_POSTFIELDS, $contact_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
$info = curl_getinfo($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($server_output, 0, $header_size);
$response = substr($server_output, $header_size);
if($info['http_code'] == 201) {
echo "Contact created successfully, the response is given below \n";
echo "Response Headers are \n";
echo $headers."\n";
echo "Response Body \n";
echo "$response \n";
}
else
{
if($info['http_code'] == 404)
{
echo "Error, Please check the end point \n";
}
else
{
echo "Error, HTTP Status Code : " . $info['http_code'] . "\n";
echo "Headers are ".$headers."\n";
echo "Response is ".$response;
}
}
curl_close($ch);
?>
Also check your API key and password one more time before sending the request whether both are correct or wrong?

CLIO return just on matter number by display number

Clio seems to have no solution but to ask it here.
So here I go.
I have the display number and I want to query the Matter ID.
This does not seem to be possible unless I do I x-bulk query the loop throw the results.
Does anyone have a simple query to do this?
here is what I have for PHP so far.
function matter_one ( $token,$refresh_toke ) {
$header = 'Authorization: bearer '.$token."\r\n";
//echo $header."\r\n";
$offset = 0;
$url = 'https://app.clio.com/api/v4/matters.json?&fields=id,description,display_number';
//echo $url."\r\n";
$ch = curl_init();
//curl_setopt($ch, CURLOPT_VERBOSE, true); //used for trouble shooting.
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$resp = curl_exec($ch);
print "curl response is:" . $resp;
die();
There is a better way, use
https://app.clio.com/api/v4/matters?fields=id,display_number&query=100140
you will need to loop and check the display number does actualy match as 100140 and 100140B and 10014000 would be returned also.
If the issue is you do not want to do the x-bulk here is code that will pull all the info, you can either modify the code to find the item in the data returned for each page or find in the final array.
function performListRequestAllMatters()
{
$header = 'Authorization: bearer ' . $this->token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->base . $this->API_version . 'matters' . '?fields=id,display_number');
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
if (!$response) {
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
} elseif (200 != curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
echo "Bad Response Code!";
echo "\n";
echo "Response HTTP Status Code : " . curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "\n";
echo "Response HTTP Body : " . $response;
} else {
$json = json_decode($response, true);
$list = $json['data'];
while (isset($json['meta']['paging']['next'])) {
// process a new request until all pages have been retrieved
curl_setopt($ch, CURLOPT_URL, $json['meta']['paging']['next']);
$response = curl_exec($ch);
$json = json_decode($response, true);
$newList = $json['data'];
$list = array_merge($list, $newList);
}
}
curl_close($ch);
return $list;
}
if have 10,000 plus records you have to use bulk.
this function works great at getting all the records.
Then you can find what ever number you need.
function matter_ID_display ( $token,$refresh_toke ) {
// this returns all the matter ID and display name
//adding X-BULK
$header = 'Authorization: bearer '.$token."\r\n"."X-BULK: true";
//echo $header."\r\n";
$offset = 0;
$url = 'https://app.clio.com/api/v4/matters.json?offset='.$offset.'&fields=id,display_number';
$cookie_jar = 'C:\Sites\Project for simmons and flecter\PHP\tmp\cookies.txt';
$cookie_jar2 = 'C:\Sites\Project for simmons and flecter\PHP\tmp\cookies2.txt';
//echo $url."\r\n";
$ch = curl_init();
//curl_setopt($ch, CURLOPT_VERBOSE, true); //used for trouble shooting.
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$resp = curl_exec($ch);
//print "curl response is:" . $resp;
if( !$resp ) {
die('Error: "' . curl_error( $ch ) . '" - Code: ' . curl_errno( $ch ) );
}
else if ( 200 != curl_getinfo( $ch, CURLINFO_HTTP_CODE ) ) {
echo "Expected code 202";
echo "\n";
echo "Response HTTP Status Code : " . curl_getinfo( $ch, CURLINFO_HTTP_CODE );
echo "\n";
//echo "Response HTTP Body : " . $resp;
}
//$resp = json_encode($resp);
$headers = get_headers_from_curl_response($resp);
//$headers = json_encode($headers);
//print_r ($headers);
$url = $headers["Location"];
echo "url for getting the bulk data\r\n";
echo $url."\r\n";
echo "\n";
curl_close($ch);
//sleep for x seconds
//have to sleep due to processing at clio
$seconds = 20;
echo "Sleeping for ".$seconds." seconds\r\n";
sleep($seconds);
echo "\n";
// seperate curl_
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_COOKIEJAR => $cookie_jar2,
CURLOPT_COOKIEFILE=> $cookie_jar2,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array("authorization: Bearer ".$token),
CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_RETURNTRANSFER => true, // return web page
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo 'Curled first time';
echo "\n";
// have to curl two times one without authorization
echo "\n";
Echo "cURL repeat";
echo "\n";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_COOKIEJAR => $cookie_jar2,
CURLOPT_COOKIEFILE=> $cookie_jar2,
CURLOPT_CUSTOMREQUEST => "GET",
//CURLOPT_HTTPHEADER => array("authorization: Bearer ".$token),
CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_RETURNTRANSFER => true, // return web page
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else
{echo 'Good Reponse from Curl second time';
echo "\n";}
}
$response_Decode = json_decode($response);
//print_r ($response_Decode);
// got the data. Now just need to turn to a clean array and save to file.
//die('Before Array clean up');
//clean and make into an array
$response_Decode=$response_Decode->data;
$response_Decode=$response_Decode[0];
//print_r ($response_Decode);
//die(); //For testing
$matter_array = array();
if(!empty($response_Decode->data) && is_array($response_Decode->data)) {
foreach ($response_Decode->data as $info) {
$d = array();
$d[] = $info->display_number;
$d[] = $info->id;
$matter_array[] = $d;
}
}
print_r($matter_array); //For testing
die(); //For testing
//matter_array is a full array of all matter ID and Matter Display numbers
return $matter_array; }

Basic Authorization PHP with Server related

I have some problem that related to HTTP_HEADERS in curl php in opencart. The code below is caller.
$ch = curl_init();
$url = 'http://aaa.com/index.php?route=common/home/getTotalCustomer';
$url2 = 'http://bbb.com/index.php?route=common/home/getTotalCustomer';
$url3 = 'http://ccc.com/index.php?route=common/home/getTotalCustomer';
$header = array('Authorization:Basic ' . base64_encode('user:password'));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$results = curl_exec($ch);
echo '<pre>';
print_r(json_decode($results, true));
echo '</pre>';
The receiver code like below:
public function getTotalCustomer(){
$json = array();
$this->load->model('account/customer');
if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])){
if(($_SERVER['PHP_AUTH_PW'] == 'password') && ($_SERVER['PHP_AUTH_USER'] == 'user')){
$json['total_customer'] = $this->model_account_customer->getTotalCustomer();
}
} else{
$json['message'] = 'failed';
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
I had tried in multiple domain with different servers. Some server can return the data but some server cannot return the data. Why?
Your header that you're sending is incorrect.
You're passing
Authorization:Basic <username:password>
it should be
Authorization: Basic <username:password>
note the space.

How to get respose for appopen event at parse.com

I am getting blank json in response when appOpen event is called
My code : -
$headers = array(
"X-Parse-Application-Id:trPYPHrI1Cfb8Q4w3TQ5HY7RoDcOFSM4okdNWDyC" ,
"X-Parse-REST-API-Key:OeNTANdx5A6ILHIZi3NfVcuE3GPDg4Cmyo9aQM1R",
"Content-Type: application/json"
);
$data='{ }';
$rest = curl_init();
curl_setopt($rest,CURLOPT_URL,"https://api.parse.com/1/events/AppOpened");
curl_setopt($rest,CURLOPT_HTTPHEADER,$headers);
curl_setopt($rest, CURLOPT_POST,true);
curl_setopt($rest, CURLOPT_RETURNTRANSFER, true);
curl_setopt($rest, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($rest);
echo $response;
curl_close($rest);
output : { }
working url : http://norefresh.thesparxitsolutions.com/rajeevranjan/parse/appopen.php
please help.
This URL https://api.parse.com/1/events/AppOpened
return {} or true means your app lauched. You can check responsecode by
$responseCode = curl_getinfo($rest, CURLINFO_HTTP_CODE);
echo "<pre>";
print_r($responseCode);
echo "</pre>";
I have checked with other url https://api.parse.com/1/classes/Player, as it's working fine. check following code:
$headers = array(
"X-Parse-Application-Id:trPYPHrI1Cfb8Q4w3TQ5HY7RoDcOFSM4okdNWDyC" ,
"X-Parse-REST-API-Key:OeNTANdx5A6ILHIZi3NfVcuE3GPDg4Cmyo9aQM1R",
"Content-Type: application/json"
);
$data='{ }';
$rest = curl_init();
curl_setopt($rest,CURLOPT_URL,"https://api.parse.com/1/classes/Player");
curl_setopt($rest,CURLOPT_HTTPHEADER,$headers);
curl_setopt($rest, CURLOPT_POST,true);
curl_setopt($rest, CURLOPT_RETURNTRANSFER, true);
curl_setopt($rest, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($rest);
echo "<pre>";
print_r($response);
echo "</pre>";
curl_close($rest);

Schedule urbanairship push notification

After reading documentation I have tried to write the following source:
$contents = array();
//$contents['badge'] = "+1";
$contents['alert'] = "Touchdown!";
//$contents['sound'] = "cat.caf";
$notification = array();
$notification= $contents;
$audience['tag'] = "49ers";
$scheduled['scheduled_time'] = "2013-04-01T18:45:30";
$push['push'] = array("audience"=> $audience, "notification"=>$notification, "device_types"=>"all");
$response = array();
array_push($response, array("name" => "My schedule", "schedule"=> $scheduled));
array_push($response, $push);
$json = json_encode($response);
//echo "Payload: " . $json . "\n"; //show the payload
$session = curl_init(PUSHURL);
curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
curl_setopt($session, CURLOPT_POST, True);
curl_setopt($session, CURLOPT_POSTFIELDS, $json);
curl_setopt($session, CURLOPT_HEADER, False);
curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
$content = curl_exec($session);
// echo "Response: " . $content . "\n";
print_r($content);
// Check if any error occured
$response = curl_getinfo($session);
if($response['http_code'] != 202) {
$error = "Got negative response from server: " . $response['http_code'] . "\n";
} else {
$success = "Message has been sent successfully";
}
I am getting error = 0.
Can anyone help me please?
Thanks.
I have figured out the problem with the help of #dperconti (thank you very much).
I am posting a solution to 'how to schedule push for urbanairship' so others may find it helpful.
Code is:
define('APPKEY','xxxx'); // Your App Key
define('PUSHSECRET', 'xxxx'); // Your Master Secret
define('PUSHURL', 'https://go.urbanairship.com/api/schedules/');
$contents = array();
//$contents['badge'] = "+1";
$contents['alert'] = "Touchdown!";
//$contents['sound'] = "cat.caf";
$notification = array();
$notification= $contents;
$audience = "all";
$scheduled['scheduled_time'] = "2016-04-01T18:45:30";
// $push['push'] = array("audience"=> $audience, "notification"=>$notification, "device_types"=>"all");
$response = array();
array_push($response, array("name" => "My schedule", "schedule"=> $scheduled, "push" => array("audience"=> $audience, "notification"=>$notification, "device_types"=>"all")));
// array_push($response, $push);
$json = json_encode($response);
echo "Payload: " . $json . "<br><br><br>"; //show the payload
$session = curl_init(PUSHURL);
curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
curl_setopt($session, CURLOPT_POST, True);
curl_setopt($session, CURLOPT_POSTFIELDS, $json);
curl_setopt($session, CURLOPT_HEADER, False);
curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
$content = curl_exec($session);
// echo "Response: " . $content . "\n";
print_r($content);
// Check if any error occured
$response = curl_getinfo($session);
if($response['http_code'] != 202) {
$error = "Got negative response from server: " . $response['http_code'] . "\n";
} else {
$success = "Message has been sent successfully";
}
curl_close($session);

Categories