I have the below code snippet which allows me to submit a url, an order type and a quantity via an API but I am struggling to use the function "order", within class "OrderSubmit". I have tried this but it doesn't work:
order(http://testurl.com, 1, 100);
Do I need any particular extra code to use the function because it is within a class? Any other tips where I am going wrong?
class OrderSubmit {
public $api_url = 'XXX'; // API URL
public $api_key = 'XXX'; // Your API key
/**
* Place an order
*/
public function order($link, $type, $quantity) { // Add order
return json_decode($this->connect(array(
'key' => $this->api_key,
'action' => 'add',
'link' => $link,
'type' => $type,
'quantity' => $quantity
)));
}
/**
* Get Order Status
*/
public function status($order_id) {
return json_decode($this->connect(array(
'key' => $this->api_key,
'action' => 'status',
'id' => $order_id
)));
}
/**
* Send post request
*/
private function connect($post) {
$_post = Array();
if (is_array($post)) {
foreach ($post as $name => $value) {
$_post[] = $name.'='.urlencode($value);
}
}
$ch = curl_init($this->api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if (is_array($post)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, join('&', $_post));
}
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
$result = curl_exec($ch);
if (curl_errno($ch) != 0 && empty($result)) {
$result = false;
}
curl_close($ch);
return $result;
}
}
$reseller = new OrderSubmit();
$response = $reseller -> order($link, $type, $quantity); //place an order
$response = $reseller -> status($orderid); // get the status
Answer:
Did you add the code at the bottom? Try adding the following above those lines:
$link = "http://testurl.com";
$type = 1;
$quantity = 100;
$reseller = new OrderSubmit();
$response = $reseller -> order($link, $type, $quantity); //place an order
$response = $reseller -> status($orderid); // get the status
To use any of the functions in the OrderSubmit class, you need to create an OrderSubmit object, which happens in the following line:
$reseller = new OrderSubmit();
Any actions/functions are then called on the
$reseller
variable.Answer:
Your code does work properly.
I returned
json_encode
array within connect() function to show you if its returned properly, it did print the results. Using var_dump()
to see its contents.
I would check the url/api you are connecting to be sure its a proper JSON. As for the code itself you've supplied, its correct for calling a function within a class.
example code
class OrderSubmit {
public $api_url = 'XXX'; // API URL
public $api_key = 'XXX'; // Your API key
/**
* Place an order
*/
public function order($link, $type, $quantity) { // Add order
return json_decode($this->connect(array(
'key' => $this->api_key,
'action' => 'add',
'link' => $link,
'type' => $type,
'quantity' => $quantity
)));
}
/**
* Get Order Status
*/
public function status($order_id) {
return json_decode($this->connect(array(
'key' => $this->api_key,
'action' => 'status',
'id' => $order_id
)));
}
/**
* Send post request
*/
private function connect($post) {
return json_encode(array('id'=>2));
}}
$reseller = new OrderSubmit();
$response = $reseller->order('http://test.com', 1, 100); //place an order
// $response = $reseller -> status($orderid); // get the status
var_dump($response);
in my example, returning
object(stdClass)#3 (1) { ["id"]=> int(2) }
No comments:
Post a Comment