在类中调用函数

时间:2015-09-03 22:06:19

标签: php

我有以下代码片段,它允许我通过API提交网址,订单类型和数量,但我很难在课程" OrderSubmit&中使用功能" order" #34 ;. 我试过这个但是它不起作用: 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

2 个答案:

答案 0 :(得分:0)

您是否在底部添加了代码?尝试在这些行上面添加以下内容:

$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

要使用OrderSubmit类中的任何函数,您需要创建一个OrderSubmit对象,该对象发生在以下行中:

$reseller = new OrderSubmit();

然后在$reseller变量上调用任何动作/函数。

答案 1 :(得分:0)

您的代码可以正常运行。

我在connect()函数中返回json_encode数组,以显示它是否正确返回,它确实打印了结果。使用var_dump()查看其内容。

我会检查你正在连接的url / api,以确保它是一个合适的JSON。至于你提供的代码本身,它对于调用类中的函数是正确的。

示例代码

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);

在我的例子中,返回

 object(stdClass)#3 (1) { ["id"]=> int(2) }