I want to update my custom field in freshdesk with php curl. Bash command looks like this and it works:
curl -v -u myapikey:X -H "Content-Type: application/json" -X PUT -d '{ "custom_fields": {"hours":"2" }}' 'https://myfreshdeskname.freshdesk.com/api/v2/tickets/1000'
Now im trying to create function in php that will do this for me. For now it looks like that, but it doesn't work and i don't know why:
function freshdesk($url, $apitoken) {
$data = json_encode(['custom_fields' => ['hours' => '2' ]]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_USERPWD, $apitoken . ':X');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
curl_close($ch);
return $result;}
I found what i was missing. It was a header. So i had to add smth like this to code above:
$header = "Content-Type: application/json";
And then:
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
Im leaving it here in case someone find it helpfull.
Related
How to get survey ID by name in SurveyMonkey?
This is what is found, but how to convert this into PHP?
curl -i -X POST -H "Authorization:bearer YOUR_ACCESS_TOKEN" -H "Content-Type": "application/json" https://api.surveymonkey.net/v3/surveys -d '{"title":"New Survey"}'
Here is my basic api call without passing the survey title parameter:
<?php
$requestHeaders = array(
'Content-Type: application/json',
'Authorization: Bearer 12345',
);
$url = 'https://api.surveymonkey.net/v3/surveys/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_contactlist);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders);
?>
Thanks.
Solution:
Add
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
i found the solution: add the following lines:
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
I have a cURL command which works very well en CLI (via git Bash). See below:
curl -D- -k -o tvdata.xls -u adminid:adminpw -X GET -H "Content-Type: application/vnd.ms-excel" https://localhost/jira/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+LPCCU+AND+type+%3D+Incident
This command export very well all issues in an excel file (xls) from my JIRA.
Now i want to transform this command to php curl. I have tried this code below:
$url = 'https://build.bnum.laposte.fr/jira/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+LPCCU+AND+type+%3D+Incident';
$username ='adminid';
$password ='adminpw';
$file = fopen('tvdata.xls', 'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/vnd.ms-excel"));
curl_setopt($ch, CURLOPT_NOPROGRESS, FALSE);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 15000);
curl_setopt($ch, CURLOPT_FILE, $file);
curl_exec($ch);
curl_close($ch);
fclose($file);
but when i run this php code, he create an empty excel file only, but without to get an error. If somebody can figure out this problem please?
Thanks in advance
Achillix
Have you tried using on-line converters like this one?
From...
curl -D- -k -o tvdata.xls -u adminid:adminpw -X GET -H "Content-Type: application/vnd.ms-excel" https://localhost/jira/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+LPCCU+AND+type+%3D+Incident
...you get:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://localhost/jira/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+LPCCU+AND+type+%3D+Incident");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, "adminid" . ":" . "adminpw");
$headers = array();
$headers[] = "Content-Type: application/vnd.ms-excel";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
...compared with your sample code, CURLOPT_URL and CURLOPT_HTTPHEADER are different, and some other options are not set.
I'm referencing DigitalOcean's API docs, and they give the following example on how to delete a droplet here:
curl -X DELETE -H "Content-Type: application/json" -H "Authorization: Bearer API_TOKEN" "https://api.digitalocean.com/v2/droplets/[DROPLET_ID]"
How can I write this in PHP curl?
I currently have this:
$ch = curl_init('https://api.digitalocean.com/v2/droplets/18160706');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer MY_API_TOKEN')
);
$result = curl_exec($ch);
echo $result;
But that is not deleting my droplet and is returning (Yes, the droplet id is correct):
{"id":"not_found","message":"The resource you were accessing could not be found."}1
Try This
Change the CUTLOPT_CUSTOMREQUEST to "DELETE" Find More
$ch = curl_init('https://api.digitalocean.com/v2/droplets/18160706');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer MY_API_TOKEN')
);
$result = curl_exec($ch);
echo $result;
You need to set CURLOPT_CUSTOMREQUEST to DELETE as mentioned in the PHP Manual here. Please take some time to read it. The following code should work
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$result = json_decode($result);
curl_close($ch);
I have the follwing curl call which works on linux.
curl -XPOST 'https://site.site.com/1.0/projects/1626/inbox/search.json' -H 'Content-Type: application/json' -H 'Authorization: randomauthorizationcodethatisactuallymuchlongerandmorerandom' --data '{}'
This call works when called in the terminal. I've tried to recreate it using PHP functions and I'm not having any luck. This is what I have done
$accessToken = 'randomauthorizationcodethatisactuallymuchlongerandmorerandom';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://site.site.com/1.0/projects/1626/inbox/search.json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HEADER, 'Authorization: ' . $accessToken);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$result = curl_exec($ch);
curl_close($ch);
echo $result;
I've been messing with this one for days and I can't figure it out. Any help would be appreciated. Thank you!
In LIBCURL for PHP what is the equivalent to a basic curl -d?
This is my basic CURL: I need to format it for LIBCURL in PHP:
curl -u username:password -H "Content-Type:application/json" -X POST -d '[{"phoneNumber":"12135551100","message":"Hello World!"}]' "https://api.example.com/v2/texts"
I have tried using CURLOPT_WRITEFUNCTION and CURLOPT_WRITEDATAbut I can't seem to get my request to work.
The option required is CURLOPT_POSTFIELDS. The specification is the same as the libcurl reference.
There are some PHP examples in the curl_setopt reference. The simplest way being the following example:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "postvar1=value1&postvar2=value2");
My endpoint needed data in the form of an array so this worked.
function doPost($url, $user, $password, $params = array()) {
$authentication = 'Authorization: Basic '.base64_encode("$user:$password");
$http = curl_init($url);
curl_setopt($http, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($http, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($http, CURLOPT_RETURNTRANSFER, true);
curl_setopt($http, CURLOPT_URL, $url);
curl_setopt($http, CURLOPT_POST, true);
curl_setopt($http, CURLOPT_POSTFIELDS, $params);
curl_setopt($http, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json', $authentication));
return curl_exec($http);
}
My array formatted like this:
$params = '[{"phoneNumber":"' . $ToNumber . '","message":"' . $msg . '"}]';