在此tutorial中解釋瞭如何在PHP中創建RESTful API。我的問題與標題爲的部分中的代碼有關。創建具體的API。我複製/粘貼NetBeans中的代碼,我得到關於該代碼的消息:找不到代碼中的語法錯誤在哪裏
$this->User = $User;
我從編輯得到的消息是變量$這是意外。我找不到這裏的錯誤。 謝謝。
這裏是代碼,它出現在編輯器:
class MyAPI extends API
{
protected $User;
public function __construct($request, $origin) {
parent::__construct($request);
// Abstracted out for example
$APIKey = new Models\APIKey();
$User = new Models\User();
if (!array_key_exists('apiKey', $this->request)) {
throw new Exception('No API Key provided');
} else if (!$APIKey->verifyKey($this->request['apiKey'], $origin)) {
throw new Exception('Invalid API Key');
} else if (array_key_exists('token', $this->request) &&
!$User->get('token', $this->request['token']))
throw new Exception('Invalid User Token');
}
$this->User = $User;
}
/**
* Example of an Endpoint
*/
protected function example() {
if ($this->method == 'GET') {
return "Your name is " . $this->User->name;
} else {
return "Only accepts GET requests";
}
}
}
在該行之前添加代碼,可能是您錯過了';'或'}'。這就是爲什麼'$ this'是意外的原因 – Niels
請張貼更多的代碼(錯誤周圍5-10行)。 – Jonathan
什麼代碼?你的意思是; } –