PHP add calendar event using CalDav - caldav

I am trying to add a calendar event to Plesk calendar using CalDAV
my code is: I put request using curl to calendar URL,
I got the exists events so the link not the problem, the problem is creat
my code is:
$uid = "test-12345"; // setting this to an existing uid updates event, a new uid adds event
$url = 'calender_url'.$uid.'.ics'; //http://mail.domain.com/calendars/DOMAIN/USER/Calendar/'.$uid.'.ics'
$userpwd = "user:password";
$description = 'My event description here';
$summary = 'My event title 1';
$tstart = '202006015T000000Z';
$tend = '20200616T000000Z';
$tstamp = gmdate("Ymd\THis\Z");
$body = "<<<__EOD
BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
DTSTAMP:$tstamp
DTSTART:$tstart
DTEND:$tend
UID:$uid
DESCRIPTION:$description
LOCATION:Office
SUMMARY:$summary
END:VEVENT
END:VCALENDAR
__EOD";
$headers = array(
'Content-Type: text/calendar; charset=utf-8',
'If-None-Match: *',
'Expect: ',
'Content-Length: '.strlen($body),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $userpwd);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$exe = curl_exec($ch);
curl_close($ch);
and I got next error
This resource only supports valid iCalendar 2.0 data. Parse error: This parser only supports VCARD and VCALENDAR files
any advice?
thanks in advance.

You should replace
"<<<__EOD
with
<<<__EOD
and
__EOD";
with
__EOD;

PHP_EOL and TRIM() help me:
$calendarData = "
BEGIN:VCALENDAR".PHP_EOL."
VERSION:2.0".PHP_EOL."
BEGIN:VEVENT".PHP_EOL."
DTSTAMP:$tstamp".PHP_EOL."
DTSTART:$tstart".PHP_EOL."
DTEND:$tend".PHP_EOL."
UID:$uid".PHP_EOL."
DESCRIPTION:$description".PHP_EOL."
LOCATION:Office".PHP_EOL."
SUMMARY:$summary".PHP_EOL."
END:VEVENT".PHP_EOL."
END:VCALENDAR
";
$calendarData = trim($calendarData);
and watch carefully that the beginning of each line starts with a parameter and not a space or indentation.
And.. now.. we can use (example to use in APP)
$etag = $this->calDavBackEnd->createCalendarObject($calendarId, $url, $calendarData);

Related

Getting data from Kobo Toolbox

I want to get my form data back from kobo api (https://kc.kobotoolbox.org/api/v1/). I registered client application from kobo. To authorize client application, i run below url but it results in
http://localhost/kobo/o/authorize?client_id=MY_CLIENT_iD&response_type=code&state=xyz
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.27 (Win32) OpenSSL/1.0.2l PHP/7.1.9
Similarly request for access token also result in same error
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/o/token/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=authorization_code&\ncode=PSwrMilnJESZVFfFsyEmEukNv0sGZ8&\nclient_id=MY_CLIENT_ID&\nredirect_uri=http://localhost:30000");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "USERNAME" . ":" . "PASSWORD");
$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
echo $result;
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.27 (Win32) OpenSSL/1.0.2l PHP/7.1.9
I am using xampp and curl is enabled.
You have to use https://kc.kobotoolbox.org/api/v1/ not https://kf.kobotoolbox.org/api/v1/
checkout docs here http://help.kobotoolbox.org/managing-your-project-s-data/rest-services
MY WORKING CODE:
$url = 'https://kc.kobotoolbox.org/api/v1/stats';
$data = array (
// 'q' => 'nokia'
);
$params = '';
foreach($data as $key=>$value)
$params .= $key.'='.$value.'&';
$params = trim($params, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.'?'.$params ); //Url together with parameters
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 7); //Timeout after 7 seconds
//curl_setopt($ch, CURLOPT_HEADER , false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: Token xxxxxxxx';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//curl_setopt($ch, CURLOPT_USERPWD, "USERNAME" . ":" . "PASSWORD");
$result = curl_exec($ch);
if(curl_errno($ch)) //catch if curl error exists and show it
echo 'Curl error: ' . curl_error($ch);
echo $result;

Upload a file into dropbox folder

I have tried this to upload a file from a folder in my desktop to a folder in dropbox account.
but each time i have uploaded an empty file through this code.
How it is possible?
Below s my code:
$ch = curl_init();
$TOKEN = "asasasa";//token here
$url = 'https://content.dropboxapi.com/2/files/upload';
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: multipart/form-data';
$headers[] = 'Dropbox-API-Arg:{"path":"/home/new/ofd","mode":{".tag":"add"},"autorename":false,"mute":false}';
$headers[] = "Authorization: Bearer ".$TOKEN;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$droplist = json_decode($response,true);
You don't seem to be adding the file content to the upload call anywhere, so an empty file is expected from your code.
You can find an example of using /2/files/upload with curl in PHP below. That uses CURLOPT_INFILE to add the file content itself.
<?php
$path = 'test_php_upload.txt';
$fp = fopen($path, 'rb');
$size = filesize($path);
$cheaders = array('Authorization: Bearer <ACCESS_TOKEN>',
'Content-Type: application/octet-stream',
'Dropbox-API-Arg: {"path":"/test/'.$path.'", "mode":"add"}');
$ch = curl_init('https://content.dropboxapi.com/2/files/upload');
curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo $response;
curl_close($ch);
fclose($fp);
?>
<ACCESS_TOKEN> should be replaced with the OAuth 2 access token.
Upload a file into dropbox folder dropbox api v2
$dropbox_api_url = 'https://content.dropboxapi.com/2/files/upload'; //dropbox api url
$token = 'Access token value put here'; // should be replaced with the OAuth 2 access token
$headers = array('Authorization: Bearer '. $token,
'Content-Type: application/octet-stream',
'Dropbox-API-Arg: '.
json_encode(
array(
"path"=> '/'. basename($filename),
"mode" => "add",
"autorename" => true,
"mute" => false
)
)
);
$ch = curl_init($dropbox_api_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
$path = $filename;
$fp = fopen($path, 'rb');
$filesize = filesize($path);
curl_setopt($ch, CURLOPT_POSTFIELDS, fread($fp, $filesize));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//For display value as array object starts here
echo "<pre>";
print_r(json_decode($response));
//For display value as array object ends here
echo($response.'<br/>');
echo($http_code.'<br/>');
curl_close($ch);

PayPal PayFlow API Error

We are using PayFlow PayPal API for recurring payments, it gives us an issue.
We are sending amount $29 to the API but on PayPal log it is showing $1 only. When we asked PayPal support for this they said you are sending $1.
Please help us. Below is the code we have setup:
<?php
$payflow_partner = 'PayPal';
$payflow_vender = 'xxxxxxxxxxxxx';
$payflow_user = 'xxxxxxxxxxxx';
$payflow_pwd = 'XXXXXXXXX';
$payflow_url = 'https://pilot-payflowpro.paypal.com';
$first_name = 'First Name';
$last_name = 'Last Name';
$profile_name = $first_name.$last_name;
$plan_amount = 29.00;
$card_number = '4111111111111111';
$expiry_month = '05';
$expiry_year = '21';
$expiry = $expiry_month.$expiry_year;
$user_email = 'test#example.com';
$start_date = '01202017';
$post_list = 'TRXTYPE=R&TENDER=C&PARTNER='.$payflow_partner.'&VENDOR='.$payflow_vender.'&USER='.$payflow_user.'&PWD='.$payflow_pwd.'&ACTION=A&PROFILENAME='.$profile_name.'&AMT='.$plan_amount.'&CURRENCY=USD&ACCT='.$card_number.'&EXPDATE='.$expiry.'&START='.$start_date.'&PAYPERIOD=MONT&TERM=0&EMAIL='.$user_email.'&OPTIONALTRX=A&OPTIONALTRXAMT='.$plan_amount.'&COMMENT1=First-time-customer&STREET=sector-7-malviya-nagar&ZIP=302017&CITY=jaipur&STATE=rajasthan&COUNTRY=india&FIRSTNAME='.$first_name.'&MIDDLENAME='.$last_name.'&LASTNAME='.$last_name;
$headers = array();
$headers[] = "Content-Type: text/namevalue"; //or maybe text/xml
$headers[] = "X-VPS-Timeout: 3000";
$headers[] = "X-VPS-VIT-OS-Name: Linux"; // Name of your OS
$headers[] = "X-VPS-VIT-OS-Version: RHEL 4"; // OS Version
$headers[] = "X-VPS-VIT-Client-Type: PHP/cURL"; // What you are using
$headers[] = "X-VPS-VIT-Client-Version: 0.01"; // For your info
$headers[] = "X-VPS-VIT-Client-Architecture: x86"; // For your info
$headers[] = "X-VPS-VIT-Client-Certification-Id:13fda2433fc2123d8b191d2d011b7fdc";
$headers[] = "X-VPS-VIT-Integration-Product: MyApplication"; // For your info, would populate with application name
$headers[] = "X-VPS-VIT-Integration-Version: 0.01"; // Application version
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $payflow_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1); // tells curl to include headers in response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 45); // times out after 45 secs
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // this line makes it work under https
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_list); //adding POST data
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //verifies ssl certificate
curl_setopt($ch, CURLOPT_FORBID_REUSE, TRUE); //forces closure of connection when done
curl_setopt($ch, CURLOPT_POST, 1); //data sent as POST
$result = curl_exec($ch);
$headers = curl_getinfo($ch);
curl_close($ch);
echo $result;
In your post data you are sending these two values:
AMT='.$plan_amount
OPTIONALTRXAMT='.$plan_amount
PayPal's documentation states that OPTIONALTRXAMT should only be used when OPTIONALTRX=S, which it is not in your case.
The documentation also states that Amount is ignored when OPTIONALTRX=A, which is what you are doing.
Note: Do not specify an amount when OPTIONALTRX=A. The amount is ignored.
So, remove the Optional parameters.

PUT request to REST api using cURL

I am trying to do a put request to a RESTapi using cURL. I was able to do post request. But PUT is not working
I used following code
public function callTeamworkPutApi($secretApiKey, $apiCallString,$param)
{
//cURL
$password = "xxx";
$channel = curl_init();
//options
curl_setopt($channel, CURLOPT_URL, "http://projects.abounde.com/".$apiCallString); // projects.json?status=LATE gets all late projects
$headers = array();
$headers[] = "Authorization: Basic " . base64_encode($secretApiKey . ":" . $password);
$headers[] = "Content-Type: application/json";
curl_setopt($channel, CURLOPT_HTTPHEADER, $headers);
curl_setopt($channel, CURLOPT_PUT, true);
//curl_setopt($channel, CURLOPT_POSTFIELDS, $comment);
curl_setopt($channel, CURLOPT_PUTFIELDS, $param);
// curl_setopt($channel, CURLOPT_RETURNTRANSFER, true);
$msg = curl_exec($channel);
$tester =var_dump(curl_getinfo($channel));
curl_close($channel);
//var_dump($msg);
return response()->json(['res'=>$tester]);
}
Error:
Any Idea?
Just been doing that myself today. here is example code.
$data = array("a" => $a);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
if (!$response)
{
return false;
}
src: http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl

POST data to AtTask's API?

I'm using AtTask's API with PHP and cURL.
Is there a way to POST data instead of appending it to the end of the URL with a question mark?
I know that I can change the request name itself like CURLOPT_CUSTOMREQUEST => 'POST' and I tried adding CURLOPT_POST => true
However, the CURLOPT_POSTFIELDS => array('name' => 'Untitled Project') is still ignored.
Did anyone work with this?
Pretty sure you need to wrap your postfields array with http_build_query(). Here is an example for logging in that works for me (when I put in my username and password):
$URL = 'https://hub.attask.com/attask/api/login';
$username = 'admin';
$password = 'user';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
'username'=>$username,
'password'=>$password
)));
$results = curl_exec($ch);
curl_close($ch);
print_r($results);
Hope that helps.
I just ran into the same problem you did. It's been a few months since you asked the question, but if you're still running into it (or for anyone else hitting this wall), the issue is that you need to set the proper CURL options for POST/GET.
For a GET you'll need to set CURLOPT_HTTPGET to "true". It just makes sure the headers are in the correct order for the AtTask API server.
I've just created a github repo for the changes I've made to their sample StreamClient class.
https://github.com/angrychimp/php-attask
Feel free to use that or just lift the code from it. For your login example, GreenJimmy's example is 100% accurate. For your search question, you'll need to do something like the following.
$URL = 'https://hub.attask.com/attask/api/v4.0/';
$username = 'admin';
$password = 'pass';
$URL .= "task/search/?sessionID=$sessionID&ID=$taskID";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HTTPGET, true);
$results = curl_exec($ch);
curl_close($ch);
print_r($results);
The StreamClient in my github repo allows for arrays to be used for search params and response fields. It does make things a lot easier. For example.
require_once('StreamClient.php');
$client = new StreamClient('https://hub.attask.com', '4.0');
$client->login('admin', 'pass');
$records = $client->search('task', array('ID' => $taskID), array('assignedToID:emailAddr','actualWork'));
var_dump($records);