Created
April 24, 2012 23:13
-
-
Save hotestimator/2484506 to your computer and use it in GitHub Desktop.
Quickbooks Web Connector in PHP, raw.
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
<?php | |
require('includes/init.php'); | |
/** | |
* QB_xml_request | |
* | |
* This class turns the posted xml file into a | |
* key/value pair object we can work with. | |
* | |
* @param xml object array | |
* $request->xml->WSDLFUNCTION | |
* $reuqest->xml->WSDLFUNCTION->param | |
*/ | |
class QB_xml_request | |
{ | |
public $xml; | |
public function __construct($post_xml) | |
{ | |
$this->xml = $post_xml; | |
} | |
} | |
/** | |
* QB_response_auth | |
* | |
* This class builds and returnds the authenticateResult object to the | |
* quickbooks web connector. | |
* | |
* @param string $ticket | |
* The first member of the array is a session token, which could be a GUID or anything | |
* else that you want to use to identify the session. This token will be returned by QBWC | |
* in subsequent callbacks in the session. | |
* | |
* @param string $return_code | |
* The second member of the string array can contain a variety of things: | |
* | |
* a. If the username and password in the authenticate call is invalid, you would supply | |
* the value 'nvu'. | |
* | |
* b. If on the other hand the user data is valid but you have no work to do for that user, | |
* you would supply the value 'none'. | |
* | |
* c. If you do have work to do for the that user, you can supply the full pathname of the | |
* company to be used in the current update. | |
* | |
* d. If you want to use whatever QuickBooks company is currently open at the client | |
* end, simply supply an empty string. | |
* | |
* @param int [optional] $next_update | |
* The optional third member of the string array contains the number of seconds to | |
* wait before the next update. You would use this to in effect tell that QBWC client | |
* not to bother you for a specified time. | |
* | |
* @param int [optional] $interval | |
* The optional fourth member of the string array contains the number of seconds to | |
* be used as the MinimumRunEveryNSeconds time for your web service, which tells | |
* QBWC how frequently your web service needs to be contacted | |
* | |
* @return object authenticateResult provides authenticateResponse to qbwc | |
*/ | |
class QB_response_auth | |
{ | |
public $authenticateResult; | |
public function __construct($ticket, $return_code, $next_update = null, $interval = null) | |
{ | |
$this->authenticateResult = array($ticket, $return_code); | |
if ((int) $next_update) | |
{ | |
$this->authenticateResult[] = (int) $next_update; | |
} | |
if ((int) $interval) | |
{ | |
$this->authenticateResult[] = (int) $interval; | |
} | |
} | |
} | |
/** | |
* This function takes the user and password provided | |
* by the web connector and validates them. | |
* | |
* It will then call the QB_response_auth class to return tru or false | |
* back to quickbooks. | |
* | |
* @param string $ticket | |
* @param string $return_code | |
* @param int [optional] $next_update | |
* @param int [optional] $interval | |
* @return calls QB_response_auth object | |
*/ | |
function authenticate($request) | |
{ | |
global $db; | |
$username = $request->xml->authenticate->strUserName; | |
$password = $request->xml->authenticate->strPassword; | |
$ticket = session_id(); | |
$return_code = 'none'; | |
// validate user | |
$sql = 'SELECT * FROM users WHERE username = ' . $db->qstr($username) . ' AND password = ' . $db->qstr($password); | |
$rs = $db->Execute($sql); | |
if ($rs->RecordCount() < 1) | |
{ | |
$return_code = 'nva'; | |
} | |
$resp = new QB_response_auth($ticket, $return_code); | |
return $resp; | |
} | |
// make sure we have a post request that contains at least xml data | |
if (isset($_POST)) | |
{ | |
if ( ! isset($_SERVER['HTTP_CONTENT_TYPE']) || $_SERVER['HTTP_CONTENT_TYPE'] != 'text/xml; charset=utf-8' ) | |
{ | |
echo "[VL] No."; | |
exit(); | |
} | |
else | |
{ | |
// get the file contents | |
$file_contents = file_get_contents('php://input'); | |
$xml = simplexml_load_string($file_contents); | |
$post_xml = $xml->children("http://schemas.xmlsoap.org/soap/envelope/")->Body->children(""); | |
$request = new QB_xml_request($post_xml); | |
} | |
} | |
$server = new SoapServer($wsdl); | |
$server->addFunction('authenticate'); | |
authenticate($request); | |
$server->handle(); // sends output back to caller |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Proof that this works. Still need to write balance of functions and classes listed in the manual. Change user and password to ones that are not in your qwc file and watch the log file. You can see it 'not authenticate' ;-)