我有下面的代碼片斷,它允許我通過API提交URL,訂單類型和數量,但我很努力地在類「OrderSubmit」中使用函數「order」。 我都試過,但它不工作: order(http://testurl.com, 1, 100);
調用類中的函數
我是否需要任何特殊的額外的代碼使用的功能,因爲它是一個類中?任何其他提示,我錯了?
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
能否請您具體談談你的意思是「不工作」是什麼?有錯誤嗎?在你的代碼中它顯示'order($ link,$ type,$ quantity);'但在你之上說你嘗試了'order(http://testurl.com,1,100);'。如果您對這些值進行硬編碼以進行測試,則需要將URL放在引號中。 –