In this PHP code sample we will show you how to update information for a person already in the database. This request uses the PUT method and requires the Curl library.
The API documentation for the person resource can be found at:
https://www.myintervals.com/api/resource.php?r=person
//initialize variable values
$apiToken = ""; //your api token value
$personId = 80212; //the id of the person you are updating
//create the xml file to PUT
$putString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$putString .= "<person>\n";
$putString .= "<firstname>John</firstname>\n";
$putString .= "<lastname>Reeve</lastname>\n";
$putString .= "</person>\n";
//write the put string to a temp file, since curl PUT has to read from a file
$putData = tmpfile();
fwrite($putData, $putString);
fseek($putData, 0);
//intialize and send the curl request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://{$api_url}/person/$personId");
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $apiToken . ":");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml", "Content-type: application/xml"));
curl_setopt($ch, CURLOPT_INFILE, $putData);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($putString));
$response = curl_exec($ch);
curl_close($ch);
//close out the temp file
fclose($putData);
Hi, I'm trying to do this but this code doesn't work. I don't know what I'm doing wrong. could you help me?
I used this code, I only changed "$apitoken" and "$personID". I don't know if i have to change this line:
curl_setopt($ch, CURLOPT_USERPWD, $apiToken . ":"); // and write my password after ":"
help me please, I'm stuck
What do you mean "Doesn't work"? What happens?
You don't need to change the USERPWD line - your API token is effectively your password as far as the API is concerned (this avoids applications storing the user's password).
Reading that code I notice it doesn't have the API URL. Try adding the following under initialise variable values:
$api_url = 'api.myintervals.com';
Usos,
Thelem is correct about the API authentication. The API uses HTTP Basic Authentication to handle the username and password. However, the password in this case is discarded, because the API token contains the information necessary to authenticate the person making the API request. Therefore, anything can be added in the password portion. The important thing is to make sure the API token is being passed in as the username.
So the PHP sample would look something like this:
curl_setopt($ch, CURLOPT_USERPWD, "abjd87dl1z9:");
1 to 5 of 5
Comments are closed.
For more Intervals help documentation, please visit help.myintervals.com