2012-02-29 170 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