dpjpo746884
2012-02-29 02:18
浏览 154
已采纳

调用未定义的未定义函数?

So, when I try and run this line of code, I'm getting the following error:

Fatal error: Call to undefined function curl_http_api_request_() in /Applications/XAMPP/xamppfiles/htdocs/CI/application/libraries/Shopify.php on line 58

Where line 58 is specifically this line:

$response = curl_http_api_request_($method, $url, $query, $payload, $request_headers, $response_headers);

I'm not really sure why it can't call the second function. The code is below. I've got no clue and am at a loss as to what the issue is.

class Shopify
{
public $_api_key;
public $_shared_secret;
public $CI; // To hold the CI superglobal



public function __construct ()
{
    $this->_assign_libraries(); // Loads the CI superglobal and loads the config into it

    // Get values from the CI config
    $this->_api_key                     = $this->CI->config->item('api_key', 'shopify');
    $this->_shared_secret               = $this->CI->config->item('shared_secret', 'shopify');
}

public function shopify_app_install_url($shop_domain)
{
    return "http://$shop_domain/admin/api/auth?api_key=". $this->_api_key;
}


public function shopify_is_app_installed($shop, $t, $timestamp, $signature)
{
    return (md5($this->_shared_secret . "shop={$shop}t={$t}timestamp={$timestamp}") === $signature);
}


public function shopify_api_client($shops_myshopify_domain, $shops_token, $private_app=false)
{
    $password = $private_app ? $this->_shared_secret : md5($this->_shared_secret.$shops_token);
    $baseurl = "https://" . $this->_api_key . ":$password@$shops_myshopify_domain/";

    return function ($method, $path, $params=array(), &$response_headers=array()) use ($baseurl)
    {
        $url = $baseurl.ltrim($path, '/');
        $query = in_array($method, array('GET','DELETE')) ? $params : array();
        $payload = in_array($method, array('POST','PUT')) ? stripslashes(json_encode($params)) : array();
        $request_headers = in_array($method, array('POST','PUT')) ? array("Content-Type: application/json; charset=utf-8", 'Expect:') : array();

        $response = curl_http_api_request_($method, $url, $query, $payload, $request_headers, $response_headers);
        $response = json_decode($response, true);

        if (isset($response['errors']) or ($response_headers['http_status_code'] >= 400))
            throw new ShopifyApiException(compact('method', 'path', 'params', 'response_headers', 'response', 'shops_myshopify_domain', 'shops_token'));

        return (is_array($response) and (count($response) > 0)) ? array_shift($response) : $response;
    };
}

    public function curl_http_api_request_($method, $url, $query='', $payload='', $request_headers=array(), &$response_headers=array())
    {
        $url = curl_append_query_($url, $query);
        $ch = curl_init($url);
        curl_setopts_($ch, $method, $payload, $request_headers);
        $response = curl_exec($ch);
        $errno = curl_errno($ch);
        $error = curl_error($ch);
        curl_close($ch);

        if ($errno) throw new ShopifyCurlException($error, $errno);

        list($message_headers, $message_body) = preg_split("/

|

|/", $response, 2);
        $response_headers = $this->curl_parse_headers_($message_headers);

        return $message_body;
    }

        private function curl_append_query_($url, $query)
        {
            if (empty($query)) return $url;
            if (is_array($query)) return "$url?".http_build_query($query);
            else return "$url?$query";
        }

        private function curl_setopts_($ch, $method, $payload, $request_headers)
        {
            curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
            curl_setopt($ch, CURLOPT_USERAGENT, 'HAC');
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);

            if ('GET' == $method)
            {
                curl_setopt($ch, CURLOPT_HTTPGET, true);
            }
            else
            {
                curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $method);
                if (!empty($request_headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
                if (!empty($payload))
                {
                    if (is_array($payload)) $payload = http_build_query($payload);
                    curl_setopt ($ch, CURLOPT_POSTFIELDS, $payload);
                }
            }
        }

        private function curl_parse_headers_($message_headers)
        {
            $header_lines = preg_split("/
|
|/", $message_headers);
            $headers = array();
            list(, $headers['http_status_code'], $headers['http_status_message']) = explode(' ', trim(array_shift($header_lines)), 3);
            foreach ($header_lines as $header_line)
            {
                list($name, $value) = explode(':', $header_line, 2);
                $name = strtolower($name);
                $headers[$name] = trim($value);
            }

            return $headers;
        }


public function shopify_calls_made($response_headers)
{
    return shopify_shop_api_call_limit_param_(0, $response_headers);
}

public function shopify_call_limit($response_headers)
{
    return shopify_shop_api_call_limit_param_(1, $response_headers);
}

public function shopify_calls_left($response_headers)
{
    return shopify_call_limit($response_headers) - shopify_calls_made($response_headers);
}

    private function shopify_shop_api_call_limit_param_($index, $response_headers)
    {
        $params = explode('/', $response_headers['http_x_shopify_shop_api_call_limit']);
        return (int) $params[$index];
    }


/**
 * Shopify::_assign_libraries()
 *
 * Grab everything from the CI superobject that we need
 */
 public function _assign_libraries()
 {

        $this->CI =& get_instance();


        $this->CI->load->config('shopify', TRUE);

        return;

 }

UPDATE: This whole line is started off by me calling this line of code:

$shopify = $this->shopify->shopify_api_client($shops_myshopify_domain, $shops_token);

I have also updated the code above to include the entire file.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duangang79177 2012-02-29 02:32
    已采纳

    You can achieve it only by passing $this as object to anonymous function, as it has its own context:

    class example {
        public function trigger() {
            $func = $this->func();
            $func($this);
        }
    
        public function func() {
            return function($obj) {
                $obj->inner();
            };
        }
    
        public function inner() {
            die('inside inner');
        }
    }
    
    $obj = new example();
    $obj->trigger();
    

    EDIT: So in response to your problem:

    1. Change this line:

      return function ($method, $path, $params=array(), &$response_headers=array()) use ($baseurl)

    into this:

    return function ($instance, $method, $path, $params=array(), &$response_headers=array()) use ($baseurl)
    
    1. Inside anonymous function change this line:

      $response = curl_http_api_request_($method, $url, $query, $payload, $request_headers, $response_headers);

    into this:

    $response = $instance->curl_http_api_request_($method, $url, $query, $payload, $request_headers, $response_headers);
    
    1. Now shopify_api_client function will return you this ANONYMOUS FUNCTION with no error:

      $shopify = $this->shopify->shopify_api_client($shops_myshopify_domain, $shops_token);

    2. You need to call this function in this way:

      $shopify($this->shopify, ... AND HERE THE REST OF ARGUMENTS WHICH ANONYMOUS FUNCTION REQUIRE ...);

    Is it clearer now? I have never used shopify, but general way it should work is as I wrote.

    点赞 评论
  • doubo4824 2012-02-29 02:52

    If your accessing a method from outside the class you need to state it, if your accessing the method from within the class you need use $this->methodname()

    <?php
    class shopify_api{
        ...
    
        ...
    
        ...
        public function curl_http_api_request_($method, $url, $query='', $payload='', $request_headers=array(), &$response_headers=array())
        {
            $url = curl_append_query_($url, $query);
            $ch = curl_init($url);
            curl_setopts_($ch, $method, $payload, $request_headers);
            $response = curl_exec($ch);
            $errno = curl_errno($ch);
            $error = curl_error($ch);
            curl_close($ch);
    
            if ($errno) throw new ShopifyCurlException($error, $errno);
    
            list($message_headers, $message_body) = preg_split("/
    
    |
    
    |/", $response, 2);
            $response_headers = $this->curl_parse_headers_($message_headers);
    
            return $message_body;
        }
    
    }
    
    $shopify = new shopify_api();
    //--------V
    $response=$shopify->curl_http_api_request_();
    ?>
    


    Since you have updated your question have you tried changing:
    $shopify = $this->shopify->shopify_api_client($shops_myshopify_domain, $shops_token);
    

    too: (as it seems your adding extra shopify property, i cant see from yourcode where you have set & injected your methods to it)

    $shopify = $this->shopify_api_client($shops_myshopify_domain, $shops_token);
    
    点赞 评论

相关推荐 更多相似问题