Last active
August 29, 2015 14:11
-
-
Save onefriendaday/457d4098008047007080 to your computer and use it in GitHub Desktop.
API Test
This file contains hidden or 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 | |
class ApiTest { | |
private $config; | |
function __construct($config) | |
{ | |
$this->config = $config; | |
} | |
public function runTests() | |
{ | |
echo 'Orders GET list: '; | |
$return = $this->_curl_wrapper('http://'.$this->config['shopUrl'].'/api-v1/orders', 'get'); | |
echo '<br>Return: <textarea>' . $return . '</textarea>'; | |
$orders = json_decode($return); | |
if (isset($orders->error)) { | |
die($orders->error); | |
}else{ | |
echo count($orders) . ' orders found.<br>'; | |
} | |
echo 'Orders GET single: '; | |
$testid = '30114'; | |
$return = $this->_curl_wrapper('http://'.$this->config['shopUrl'].'/api-v1/orders?id=' . $testid, 'get'); | |
echo '<br>Return: <textarea>' . $return . '</textarea>'; | |
$orders = json_decode($return); | |
if (isset($orders->error)) { | |
die($orders->error); | |
}else{ | |
echo count($orders) . ' orders found.<br>'; | |
} | |
echo 'Order UPDATE to shipped: '; | |
$testid = '30114'; | |
$save = array('id' => $testid, 'status' => 'Shipped', 'tracking_number' => '123'); | |
$return = $this->_curl_wrapper('http://'.$this->config['shopUrl'].'/api-v1/orders', 'put', array('order' => $save)); | |
echo '<br>Return: <textarea>' . $return . '</textarea>'; | |
$orders = json_decode($return); | |
if (isset($orders->error)) { | |
die($orders->error); | |
}else{ | |
echo count($orders) . ' orders updated.<br>'; | |
} | |
echo 'Order UPDATE to cancelled: '; | |
$testid = '30114'; | |
$save = array('id' => $testid, 'status' => 'Cancelled'); | |
$return = $this->_curl_wrapper('http://'.$this->config['shopUrl'].'/api-v1/orders', 'put', array('order' => $save)); | |
echo '<br>Return: <textarea>' . $return . '</textarea>'; | |
$orders = json_decode($return); | |
if (isset($orders->error)) { | |
die($orders->error); | |
}else{ | |
echo count($orders) . ' orders updated.<br>'; | |
} | |
} | |
private function _curl_wrapper($urltopost, $method = 'get', $data = array()) | |
{ | |
$headers = array( | |
"Content-Type: application/json", | |
"X-API-KEY: ".$this->config['apiKey'] | |
); | |
$ch = curl_init($urltopost); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 540); | |
if ($method == 'post') { | |
curl_setopt($ch, CURLOPT_POST, TRUE); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); | |
} | |
if ($method == 'put') { | |
$headers[] = 'X-HTTP-Method-Override: PUT'; | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); | |
} | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$returndata = curl_exec($ch); | |
$curl_errno = curl_errno($ch); | |
$curl_error = curl_error($ch); | |
curl_close($ch); | |
return $returndata; | |
} | |
} | |
$test = new ApiTest(array('shopUrl' => 'apitest.ambienteprotegido.com', 'apiKey' => 'e85bf2e15a8261a2657ca37c94f4fd72')); | |
$test->runTests(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment