Planfix API: Contents of the included lib PHP file

From Planfix
Jump to: navigation, search

<?php

/**
 * Executes a request to the server
 * @param string $api_server
 * @param string $api_key
 * @param SimpleXMLElement $requestXml
 * @return array  success -  indicates a successful execution of the http-request,
 *  the result itself will be passed in response as an object of the SimpleXMLElement
 */
function apiRequest($api_server, $api_key, $api_token, $requestXml) {
	$result = array('success' => true, 'response' => null);
	$ch = curl_init($api_server);

	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // do not output the response to stdout
	curl_setopt($ch, CURLOPT_HEADER, 1);   // we get headers
	curl_setopt($ch, CURLOPT_TIMEOUT, 5);   // we set the maximum waiting time
// do not check SSL certificate
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	// do not check Host SSL certificate
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

	curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
	curl_setopt($ch, CURLOPT_USERPWD, $api_key . ':'.$api_token);

	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXml->asXML());

	$response = curl_exec($ch);
	$error = curl_error($ch);

	if ($error != "") {
		$result['success'] = false;
		$result['response'] = $error;
		return $result;
	}

	$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
	$responseBody = substr($response, $header_size);

	try {
		$result['response'] = new SimpleXMLElement($responseBody);
	} catch (Exception $e) {
		//  received broken XML
		$result['success'] = false;
		$result['response'] = 'broken xml';
	}
	return $result;
}

/**
 * check if the response is an error
 * @param SimpleXMLElement $responseXml
 */
function parseAPIError($responseXml) {
	if ($responseXml['status'] == 'error') {
		// you can see the decryption of the error code at https://shorturl.at/acpG4
		echo 'code :' . $responseXml->code;
		echo ' message: ' . $responseXml->message;
		exit();
	}
}

?>


Go To