2012-02-29 166 views
2

所以,当我尝试运行这行代码,我发现了以下错误:调用未定义的函数,该函数不是未定义的?

致命错误:调用未定义功能curl_http_api_request_()/应用程序/ XAMPP/xamppfiles/htdocs中/ CI /应用/库/ Shopify.php上线58

其中第58行是专门这一行:

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

我真的不知道为什么它不能调用第二功能。代码如下。我不知道问题在哪里,我不知道。

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 . ":[email protected]$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("/\r\n\r\n|\n\n|\r\r/", $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("/\r\n|\n|\r/", $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: 这整条生产线是由我开始了调用该行代码:

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

我也更新上面的代码,包括整个文件。

+3

由于方法/函数是在你需要声明,比如'$响应= $这个 - > curl_http_api_request_(一类。 ...)' – 2012-02-29 02:21:18

+0

@LawrenceCherone:我想你应该将其作为答案发布。 :-) – ruakh 2012-02-29 02:27:38

+0

$这是不好的,因为它会抛出这个错误:致命错误:当不在对象上下文中时使用$ this。匿名函数有它自己的上下文,所以你不能使用$这里面... – Karol 2012-02-29 02:27:46

回答

2

您只能通过这个$为对象,以匿名函数实现它,因为它有它自己的上下文:

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

编辑:因此,在回答您的问题:

  1. 变化这条线:

    返回函数($ method,$ path,$ params = array(),& $ response_headers = array())use($ baseurl)

成这样:

return function ($instance, $method, $path, $params=array(), &$response_headers=array()) use ($baseurl) 
  1. 内部匿名函数改变这一行:

    $响应= curl_http_api_request _($方法,$网址,$查询,$有效载荷,$ request_headers, $ response_headers);

到这一点:

$response = $instance->curl_http_api_request_($method, $url, $query, $payload, $request_headers, $response_headers); 
  1. 现在shopify_api_client函数将返回,没有错误此匿名函数:

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

  2. 你需要调用这个函数以这种方式:

    $ shopify($这个 - > shopify,...这里争论的REST的匿名功能REQUIRE ...);

现在更清楚了吗?我从来没有用过shopify,但它应该工作的一般方式就像我写的一样。

+0

你现在可以看看我更新它的代码吗?我不知道我很理解你的答案,我认为附加的代码使得我想要做的更清楚一些。 – mitchellwright 2012-02-29 05:58:25

+1

仅供参考PHP5.4'$ this'在匿名函数中可用:http://www.php.net/manual/en/functions.anonymous.php(在更新日志中) – 2012-02-29 06:01:23

+0

@MikeB我不在PHP5.4上,所以我确实得到了错误:致命错误:使用$ this,而不是在对象上下文中 – mitchellwright 2012-02-29 06:10:06

2

如果你从类的外部访问的方法,你需要声明它,如果你从类中访问的方法,你需要使用$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("/\r\n\r\n|\n\n|\r\r/", $response, 2); 
     $response_headers = $this->curl_parse_headers_($message_headers); 

     return $message_body; 
    } 

} 

$shopify = new shopify_api(); 
//--------V 
$response=$shopify->curl_http_api_request_(); 
?> 


既然你已经更新了你的问题有你试图改变:

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

太:(因为它似乎你增加额外shopify物业,我不能从已设置&注入的方法它yourcode看)

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

如果有人知道什么'$ shopify'会被归类为附加一个类给它让我知道,它仍然被称为变量?大声笑它的一个可变的对象右大声笑 – 2012-02-29 02:56:13

+0

也许我没有提供足够的信息。我首先调用一个不同的函数。我会更新原文。 – mitchellwright 2012-02-29 05:53:09