Create user using freshdesk PHP api - php

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?

Related

PHP and Shopify API connection

I'm trying to learn the shopify api for a project. Tried a few pasted codes on their forum, adapting my code to theirs.
The documentation says to do the authentication this way, by providing the following:
https://{username}:{password}#{shop}.myshopify.com/admin/api/{api-version}/{resource}.json
{username} — The API key that you generated
{password} — The API password
{shop} - The name that you entered for your development store
{api-version} — The supported API version you want to use
{resource} — A resource endpoint from the REST admin API
I'm trying to do a GET request on all the orders made on the store.
/ info
$API_KEY = '75c89bf******ea919ce****7702';
$PASSWORD = '2061******82d2802**f***9403';
$STORE_URL = '*****-na-***-c***.myshopify.com';
$AUTHORIZATION = base64_encode($API_KEY . ':' . $PASSWORD);
$url = 'https://' . $API_KEY . ':' . $PASSWORD . '#' . $STORE_URL . '/admin/api/2020-01/orders.json';
$header = array();
$header[] = 'Accept: application/json';
$header[] = 'Content-Type: application/json';
$header[] = 'Authorization: Basic ' . $AUTHORIZATION;
$session = curl_init();
//options
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_HTTPHEADER, $header);
curl_setopt($session, CURLOPT_GET, 1);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
//exec
$response = curl_exec($session);
curl_close($session);
print_r($response);
// error
if($response === false)
{
print_r('Curl error: ');
}
The code doesn't work at all, without any error code, completly blank, with only the first project echo showing.
I verified my API keys and they are working, I can insert them manually into chrome.
You don't need the header for authorization. Your code should like:
$API_KEY = '75c89bf******ea919ce****7702';
$PASSWORD = '2061******82d2802ff***9403';
$STORE_URL = 'porcao-na-sua-casa.myshopify.com';
$url = 'https://' . $API_KEY . ':' . $PASSWORD . '#' . $STORE_URL . '/admin/api/2020-01/orders.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE) //can check status code, requst successfully processed if return 200
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
Found the problem when I ran in terminal, did not install php-curl on this machine.
#Kshitij solution worked, just like mine, when curl was properly installed.
// Provide the required parameters like store url , endpoint etc.
function shopifyApiCall($STORE_URL ,$query = [], $endpoint, $API_KEY, $PASSWORD){
//API_Key : your API key, you can get it from your APP setup.
//Password : Your Access Token
$url = 'https://' . $API_KEY . ':' . $PASSWORD . '#' . $STORE_URL . $endpoint;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt ($ch, CURLOPT_POSTFIELDS, json_encode($query));
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$error_number = curl_errno($ch);
$error_message = curl_error($ch);
curl_close($ch);
// Return an error is cURL has a problem
if ($error_number) {
return $error_message;
} else {
// No error, return Shopify's response by parsing out the body and the headers
$response = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
// Convert headers into an array
$headers = array();
$header_data = explode("\n",$response[0]);
$headers['status'] = $header_data[0]; // Does not contain a key, have to explicitly set
array_shift($header_data); // Remove status, we've already set it above
foreach($header_data as $part) {
$h = explode(":", $part);
$headers[trim($h[0])] = trim($h[1]);
}
// Return headers and Shopify's response
//return array('headers' => $headers, 'response' => $response[1]);changed headers
return array('headers' => $header_data, 'response' => $response[1]);
}
}

php $_REQUEST not working when passing data through android

<?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

Facebook Messenger Bot curl_exec returns false

hopefully you can help me.
I created a simple Messenger Chatbot yesterday and it works fine. Then I tried to program it more complex and the result was that I don't get any responses anymore.
I deleted and created a new Facebook App and I used a simple script again. But this script won't work either.
$hubVerifyToken = 'YES-THIS-IS-MY-BOT';
$accessToken = "MY-HOPEFULLY-RIGHT-ACCESS-TOKEN-I-RENEWED-IT-SEVERAL-TIMES";
if ($_REQUEST['hub_verify_token'] === $hubVerifyToken) {
echo $_REQUEST['hub_challenge'];
exit;
}
$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
$answer = "Error";
if($message == "hi") {
$answer = "Hello";
}
$jsonData = '{
"recipient":{
"id":"' . $sender .'"
},
"message":{
"text":"' . $answer . '"
}
}';
$ch = curl_init('https://graph.facebook.com/v2.6/me/messages?access_token='.$accessToken);
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$resp = curl_exec($ch);
I checked the $resp and I found out that it return 'false'?
But I have absolutely no idea why ...
Can you please help me?
Comment if you need more infos.
Thank you.

ServiceM8 Post JSON via Curl PHP and API

Trying to send a post request to the ServiceM8 Api however when i attempt the request i get back no errors and nothing adding to the ServiceM8 api.
Here is what servicem8 docs suggest:
curl -u email:password
-H "Content-Type: application/json"
-H "Accept: application/json"
-d '{"status": "Quote", "job_address":"1 Infinite Loop, Cupertino, California 95014, United States","company_id":"Apple","description":"Client has requested quote for service delivery","contact_first":"John","contact_last":"Smith"}'
-X POST https://api.servicem8.com/api_1.0/job.json
and here is what i have:
$data = array(
"username" => "**************8",
"password" => "****************",
"job_address" => "1 Infinite Loop, Cupertino, California 95014, United States"
);
$url_send ="https://api.servicem8.com/api_1.0/job.json";
$str_data = json_encode($data);
function sendPostData($url, $post){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
return $result;
-- UPDATE TO SHOW PREVIOUS ATTEMPTS TO RESOLVE BUT HAD NO LUCK..
<?php
$data = array(
"username" => "*******************",
"password" => "**********",
"job_address" => "1 Infinite Loop, Cupertino, California 95014, United States"
);
$url_send ="https://api.servicem8.com/api_1.0/job.json";
$str_data = json_encode($data);
function sendPostData($url, $post){
$headers= array('Accept: application/json','Content-Type: application/json');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch); // Seems like good practice
return $result;
}
echo " " . sendPostData($url_send, $str_data);
?>
adding the headers as i have in that example still does nothing and does not create the record in servicem8 or show an error.
Hopefully someone can help me make the correct Curl request using the PHP libary.
Thanks
First issue is it looks like you are not setting authentication details correctly. To use HTTP basic auth in CURL:
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
Second issue is that job_status is a mandatory field when creating Jobs, so you need to make sure to include that as part of your create request.
Assuming that you receive a HTTP 200 response, then you the UUID of the record you just created is returned in the x-record-uuid header (docs). See this answer for an example of how to get headers from a HTTP response in CURL.
Here's your example code modified to include the above advice:
$data = array(
"job_address" => "1 Infinite Loop, Cupertino, California 95014, United States",
"status" => "Work Order"
);
$url_send = "https://api.servicem8.com/api_1.0/job.json";
$str_data = json_encode($data);
function sendPostData($url, $post, $username, $password) {
$ch = curl_init($url);
if ($username && $password) {
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
}
$headers = array('Accept: application/json','Content-Type: application/json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1); // Return HTTP headers as part of $result
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
// $result is the HTTP headers followed by \r\n\r\n followed by the HTTP body
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = substr($result, $header_size);
$strRecordUUID = "";
$arrHeaders = explode("\r\n", $header);
foreach ($arrHeaders as $strThisHeader) {
list($name, $value) = explode(':', $strThisHeader);
if (strtolower($name) == "x-record-uuid") {
$strRecordUUID = trim($value);
break;
}
}
echo "The UUID of the created record is $strRecordUUID<br />";
return $body;
}
echo "the response from the server was <pre>" . sendPostData($url_send, $str_data, $username, $password) . "</pre>";

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

Categories