Skip to content

Instantly share code, notes, and snippets.

@keranm
Created January 8, 2014 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keranm/8311073 to your computer and use it in GitHub Desktop.
Save keranm/8311073 to your computer and use it in GitHub Desktop.
This is a very quick CURL based function to GET a URL - specifically for the MYOB AccountRight Live API
/******************************************************************************
** GETURL
** The purpose of this function is to call any url we give it
** The function expects at least a URL
** It also can take (optionally) the username and password for the company file
**
********************************************************************************/
function getURL($url, $companyFileUsername = null, $companyFilePassword = null) {
// if we have a username we need to base64_encode it - so lets check if it's set
if( isset( $companyFileUsername ) ) {
$companyFileToken = base64_encode( $companyFileUsername.':'.$companyFilePassword );
} else {
$companyFileToken = '';
}
// we setup some headers to tell the API some information like the company file token and api version
$headers = array(
'x-myobapi-cftoken: '.$companyFileToken,
'x-myobapi-version: v2',
);
// setup the CURL session & pass it the URL we will call
$session = curl_init( $url );
// curl options
curl_setopt( $session, CURLOPT_HTTPHEADER, $headers ); // set the headers
curl_setopt( $session, CURLOPT_HEADER, false ); // tell curl NOT to return the headers (set to true to debug)
curl_setopt( $session, CURLOPT_RETURNTRANSFER, true );
// lets fire this off & get the response
$response = curl_exec( $session );
curl_close( $session ); // close the curl session to free up memory
// okay, lets pass the response back
return( $response );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment