iT邦幫忙

1

關於linebot部署

各位大大好,我部署上去heroku後,log卻一直顯示這個
"Switch statements may only contain one default clause in /app/index.php on line 76"
請問是哪裡出問題了>"<

看更多先前的討論...收起先前的討論...
為何不把code貼出來?這樣誰能幫你
上面說的真好 (真心覺得xDDD
流程步驟 參考哪裡 到哪裡卡住 要講清楚 ..
各位大大好,感謝兩津勘吉的提醒,我之前是部署在azure上,現在想轉到heroku
爬了一些文終於將下面兩個檔案部署上去了,不過還是沒有反應,
我部署了兩個檔案

一個是LINEBotTiny.php
程式碼如下:
<?php
if (!function_exists('hash_equals')) {
defined('USE_MB_STRING') or define('USE_MB_STRING', function_exists('mb_strlen'));
function hash_equals($knownString, $userString)
{
$strlen = function ($string) {
if (USE_MB_STRING) {
return mb_strlen($string, '8bit');
}
return strlen($string);
};
// Compare string lengths
if (($length = $strlen($knownString)) !== $strlen($userString)) {
return false;
}
$diff = 0;
// Calculate differences
for ($i = 0; $i < $length; $i++) {
$diff |= ord($knownString[$i]) ^ ord($userString[$i]);
}
return $diff === 0;
}
}
class LINEBotTiny
{
public function __construct($channelAccessToken, $channelSecret)
{
$this->channelAccessToken = $channelAccessToken;
$this->channelSecret = $channelSecret;
}
public function parseEvents()
{
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
error_log("Method not allowed");
exit();
}
$entityBody = file_get_contents('php://input');
if (strlen($entityBody) === 0) {
http_response_code(400);
error_log("Missing request body");
exit();
}
if (!hash_equals($this->sign($entityBody), $_SERVER['HTTP_X_LINE_SIGNATURE'])) {
http_response_code(400);
error_log("Invalid signature value");
exit();
}
$data = json_decode($entityBody, true);
if (!isset($data['events'])) {
http_response_code(400);
error_log("Invalid request body: missing events property");
exit();
}
return $data['events'];
}
public function replyMessage($message)
{
$header = array(
"Content-Type: application/json",
'Authorization: Bearer ' . $this->channelAccessToken,
);
$context = stream_context_create(array(
"http" => array(
"method" => "POST",
"header" => implode("\r\n", $header),
"content" => json_encode($message),
),
));
$response = file_get_contents('https://api.line.me/v2/bot/message/reply', false, $context);
if (strpos($http_response_header[0], '200') === false) {
http_response_code(500);
error_log("Request failed: " . $response);
}
}
private function sign($body)
{
$hash = hash_hmac('sha256', $body, $this->channelSecret, true);
$signature = base64_encode($hash);
return $signature;
}
}


另一個檔案是index.php
程式碼如下:
<?php
require_once('./LINEBotTiny.php');

$channelSecret = "*****************";

$channelAccessToken = "**********************************************";

$googledataspi = "https://spreadsheets.google.com/feeds/list/*************/od6/public/values?alt=json";

$client = new LINEBotTiny($channelAccessToken, $channelSecret);

foreach ($client->parseEvents() as $event) {
switch ($event['type']) {
case 'message':

$message = $event['message'];

$json = file_get_contents($googledataspi);
$data = json_decode($json, true);
$rand = mt_rand(1,1000);

foreach ($data['feed']['entry'] as $item) {

$number = $item['gsx$number']['$t'];

switch ($message['text']) {
case "123":
if (mb_strpos($rand, $number) !== false) {
$text_123 = $item['gsx$event']['$t'];
}
break;
default:
error_log("Unsupporeted message text: " . $message['text']);
break;
}
}
switch ($message['text']) {
case '123':
$client->replyMessage(array(
'replyToken' => $event['replyToken'],
'messages' => array(
array(
'type' => 'text',
'text' => $text_123,
),
),
));
break;
default:
error_log("Unsupporeted message text: " . $message['text']);
break;
}
break;
default:
error_log("Unsupporeted event type: " . $event['type']);
break;
}
};
先說一下抱歉 PHP我不太熟 語法可能無法幫到忙
上Heroku時可以看Log連接
請注意你的Webhook有正常與Line做連接 並且如果有錯誤可以在Heroku看見錯誤行數
如無連接則可能部屬失敗或是webhook尚未連接
Heroku Log位置 : More -> View Logs
有連結到,我有稍微爬了一下文,我是用php5.6寫的,但heroku預設php好像是7.0,我不知道該如何修正讓heroku跑php5.6,懇請高手指導
請參閱
https://devcenter.heroku.com/articles/php-support
內文有寫 他有支援5.6版
另外 你的程式碼也未看到有76行 index是否沒貼完整
新增composer.json:
{
"require": {
"php": "^5.6.0"
}
}
輸入composer update後

卻顯示
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- This package requires php ^5.6.0 but your PHP version (7.1.16) does not satisfy that requirement.
意思是說我的版本是5.6嗎?
不過我之前部署在azure上確實是用php5.6

不過我試著把composer.json裡改成它說的7.1.16版
確實成功部署上去heroku了,不過程式依然沒有反應
請問大大還有什麼方法呢?
我不確定PHP有沒有像是其他語言的LOG可以秀出來 可以讓你檢視你的程式執行到哪一行之後才斷掉沒反應或是根本沒有接到值 等等 ..

1 個回答

1

直接看你的錯誤訊息:
"Switch statements may only contain one default clause in /app/index.php on line 76"

在PHP 7之前可以在一個switch裡面放多個default

switch ($expr) {
    default:
         echo "Hello World";
         break;
    default:
         echo "Goodbye Moon!";
         break;
}

但在PHP7之後不行,會產生您提出的錯誤,看看是不是可以把Case中的其中一個default拿掉


補充:

另外針對版本設定可以參考以下資料:
你可以設定composer.json去改成5.6.0的環境,Push上去Heroku之後,

放置位置如下

hello(專案)
├── Procfile(Heroku佈署文件)
├── composer.json(指定RUNTIME)
└── web(您的主程式)
    ├── .htaccess
    └── index.php

下方程式碼會安裝5.6.0版本或以上,但不會安裝PHP7

{
  "require": {
    "php": "^5.6.0"
  }
}

PUSH之後你會看到Heroku佈署環境時的資訊

-----> Installing platform packages...
       - php (5.6.x)

我要發表回答

立即登入回答