Created
January 8, 2014 03:01
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************** | |
** 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