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

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

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)