Skip to content

Instantly share code, notes, and snippets.

@mtbottens
Created February 21, 2014 14:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtbottens/9135265 to your computer and use it in GitHub Desktop.
Save mtbottens/9135265 to your computer and use it in GitHub Desktop.
Modified code here: http://forums.3dcart.com/32003-post2.html into a class. Thank you jeff :)
<?php
class Soap3d {
//Ensures only the right data is passed into the soap call, incase I needed to add more into the construct $config variable
private $whiteList = array(
"storeUrl" => "",
"userKey" => ""
);
//Stores the userKey, storeUrl and the soap connection
private $conn = array();
public function __construct($config) {
$this->conn = array_intersect_key($config, $this->whiteList);
$this->conn["db"] = new soapclient('http://api.3dcart.com/cart_advanced.asmx?WSDL',array('trace'=>1,'soap_version'=>SOAP_1_1));
}
public function query($sql) {
//We only want storeUrl and userKey
$queryData = array_intersect_key($this->conn, $this->whiteList);
$queryData["sqlStatement"] = $sql;
$result = $this->conn["db"]->runQuery($queryData);
// parse results. Return error code or array of results.
if (is_soap_fault($result)) {
$array['0'] = "Soap Fault";
$array = array_merge($array, htmlspecialchars($result, ENT_QUOTES));
} else {
$pos = strpos($this->conn["db"]->__getLastResponse(), "Error");
if ($pos == true) {
$array['0'] = "SQL Error: " . $this->conn["db"]->__getLastResponse();
} else {
// create an array from soap call.
$netresult = $result->runQueryResult->any;
$netresult = str_replace("<![CDATA[", '', $netresult);
$netresult = str_replace("]]>", '', $netresult);
$array=json_decode(str_replace(':{}',':null', json_encode(simplexml_load_string($netresult,null,LIBXML_NOCDATA))),true);
}
}
if (array_key_exists('0',$array)) {
return $array;
} elseif (array_key_exists('0',$array['runQueryRecord'])) {
return $array['runQueryRecord'];
} else {
$result_array['0'] = $array['runQueryRecord'];
return $result_array;
}
}
public function execute($sql, $callback) {
$data = $this->query($sql);
if ( is_array($data[0]) ) {
foreach( $data as $key => $value ) $callback($value);
} else {
//SQL ERROR
//Guess you could put a callback here too, or just report an error.
}
}
}
//Create an instance of the class
$d = new Soap3d(array(
"storeUrl" => "xxxxxxx",
"userKey" => "xxxxxxxx"
));
//Execute with callback
$d->execute("SELECT TOP 10 * FROM orders", function($data) {
echo $data["orderid"] . "<br>";
});
//Or just run a query
$results = $d->query("UPDATE orders SET blahblah = 'thing' WHERE orderid = 123456789");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment