Overview

Namespaces

  • LINE
    • LINEBot
      • Constant
      • Event
        • MessageEvent
        • Parser
      • Exception
      • HTTPClient
      • ImagemapActionBuilder
      • MessageBuilder
        • Imagemap
        • TemplateBuilder
      • TemplateActionBuilder

Classes

  • LINE\LINEBot
  • LINE\LINEBot\Constant\ActionType
  • LINE\LINEBot\Constant\EventSourceType
  • LINE\LINEBot\Constant\HTTPHeader
  • LINE\LINEBot\Constant\MessageType
  • LINE\LINEBot\Constant\Meta
  • LINE\LINEBot\Constant\TemplateType
  • LINE\LINEBot\Event\BaseEvent
  • LINE\LINEBot\Event\BeaconDetectionEvent
  • LINE\LINEBot\Event\FollowEvent
  • LINE\LINEBot\Event\JoinEvent
  • LINE\LINEBot\Event\LeaveEvent
  • LINE\LINEBot\Event\MessageEvent
  • LINE\LINEBot\Event\MessageEvent\AudioMessage
  • LINE\LINEBot\Event\MessageEvent\ImageMessage
  • LINE\LINEBot\Event\MessageEvent\LocationMessage
  • LINE\LINEBot\Event\MessageEvent\StickerMessage
  • LINE\LINEBot\Event\MessageEvent\TextMessage
  • LINE\LINEBot\Event\MessageEvent\VideoMessage
  • LINE\LINEBot\Event\Parser\EventRequestParser
  • LINE\LINEBot\Event\PostbackEvent
  • LINE\LINEBot\Event\UnfollowEvent
  • LINE\LINEBot\HTTPClient\Curl
  • LINE\LINEBot\HTTPClient\CurlHTTPClient
  • LINE\LINEBot\ImagemapActionBuilder\AreaBuilder
  • LINE\LINEBot\ImagemapActionBuilder\ImagemapMessageActionBuilder
  • LINE\LINEBot\ImagemapActionBuilder\ImagemapUriActionBuilder
  • LINE\LINEBot\MessageBuilder\AudioMessageBuilder
  • LINE\LINEBot\MessageBuilder\Imagemap\BaseSizeBuilder
  • LINE\LINEBot\MessageBuilder\ImagemapMessageBuilder
  • LINE\LINEBot\MessageBuilder\ImageMessageBuilder
  • LINE\LINEBot\MessageBuilder\LocationMessageBuilder
  • LINE\LINEBot\MessageBuilder\MultiMessageBuilder
  • LINE\LINEBot\MessageBuilder\StickerMessageBuilder
  • LINE\LINEBot\MessageBuilder\TemplateBuilder\ButtonTemplateBuilder
  • LINE\LINEBot\MessageBuilder\TemplateBuilder\CarouselColumnTemplateBuilder
  • LINE\LINEBot\MessageBuilder\TemplateBuilder\CarouselTemplateBuilder
  • LINE\LINEBot\MessageBuilder\TemplateBuilder\ConfirmTemplateBuilder
  • LINE\LINEBot\MessageBuilder\TemplateMessageBuilder
  • LINE\LINEBot\MessageBuilder\TextMessageBuilder
  • LINE\LINEBot\MessageBuilder\VideoMessageBuilder
  • LINE\LINEBot\Response
  • LINE\LINEBot\SignatureValidator
  • LINE\LINEBot\TemplateActionBuilder\MessageTemplateActionBuilder
  • LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder
  • LINE\LINEBot\TemplateActionBuilder\UriTemplateActionBuilder

Interfaces

  • LINE\LINEBot\HTTPClient
  • LINE\LINEBot\ImagemapActionBuilder
  • LINE\LINEBot\MessageBuilder
  • LINE\LINEBot\MessageBuilder\TemplateBuilder
  • LINE\LINEBot\TemplateActionBuilder

Exceptions

  • LINE\LINEBot\Exception\CurlExecutionException
  • LINE\LINEBot\Exception\InvalidEventRequestException
  • LINE\LINEBot\Exception\InvalidEventSourceException
  • LINE\LINEBot\Exception\InvalidSignatureException
  • LINE\LINEBot\Exception\UnknownEventTypeException
  • LINE\LINEBot\Exception\UnknownMessageTypeException
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: /**
  4:  * Copyright 2016 LINE Corporation
  5:  *
  6:  * LINE Corporation licenses this file to you under the Apache License,
  7:  * version 2.0 (the "License"); you may not use this file except in compliance
  8:  * with the License. You may obtain a copy of the License at:
  9:  *
 10:  *   https://www.apache.org/licenses/LICENSE-2.0
 11:  *
 12:  * Unless required by applicable law or agreed to in writing, software
 13:  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14:  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 15:  * License for the specific language governing permissions and limitations
 16:  * under the License.
 17:  */
 18: 
 19: namespace LINE\LINEBot\HTTPClient;
 20: 
 21: use LINE\LINEBot\Constant\Meta;
 22: use LINE\LINEBot\Exception\CurlExecutionException;
 23: use LINE\LINEBot\HTTPClient;
 24: use LINE\LINEBot\Response;
 25: 
 26: /**
 27:  * Class CurlHTTPClient.
 28:  *
 29:  * A HTTPClient that uses cURL.
 30:  *
 31:  * @package LINE\LINEBot\HTTPClient
 32:  */
 33: class CurlHTTPClient implements HTTPClient
 34: {
 35:     /** @var array */
 36:     private $authHeaders;
 37:     /** @var array */
 38:     private $userAgentHeader = ['User-Agent: LINE-BotSDK-PHP/' . Meta::VERSION];
 39: 
 40:     /**
 41:      * CurlHTTPClient constructor.
 42:      *
 43:      * @param string $channelToken Access token of your channel.
 44:      */
 45:     public function __construct($channelToken)
 46:     {
 47:         $this->authHeaders = [
 48:             "Authorization: Bearer $channelToken",
 49:         ];
 50:     }
 51: 
 52:     /**
 53:      * Sends GET request to LINE Messaging API.
 54:      *
 55:      * @param string $url Request URL.
 56:      * @return Response Response of API request.
 57:      */
 58:     public function get($url)
 59:     {
 60:         return $this->sendRequest('GET', $url, [], []);
 61:     }
 62: 
 63:     /**
 64:      * Sends POST request to LINE Messaging API.
 65:      *
 66:      * @param string $url Request URL.
 67:      * @param array $data Request body.
 68:      * @return Response Response of API request.
 69:      */
 70:     public function post($url, array $data)
 71:     {
 72:         return $this->sendRequest('POST', $url, ['Content-Type: application/json; charset=utf-8'], $data);
 73:     }
 74: 
 75:     /**
 76:      * @param string $method
 77:      * @param string $url
 78:      * @param array $additionalHeader
 79:      * @param array $reqBody
 80:      * @return Response
 81:      * @throws CurlExecutionException
 82:      */
 83:     private function sendRequest($method, $url, array $additionalHeader, array $reqBody)
 84:     {
 85:         $curl = new Curl($url);
 86: 
 87:         $headers = array_merge($this->authHeaders, $this->userAgentHeader, $additionalHeader);
 88: 
 89:         $options = [
 90:             CURLOPT_CUSTOMREQUEST => $method,
 91:             CURLOPT_HTTPHEADER => $headers,
 92:             CURLOPT_HEADER => false,
 93:             CURLOPT_RETURNTRANSFER => true,
 94:             CURLOPT_BINARYTRANSFER => true,
 95:             CURLOPT_HEADER => true,
 96:         ];
 97: 
 98:         if ($method === 'POST') {
 99:             if (empty($reqBody)) {
100:                 // Rel: https://github.com/line/line-bot-sdk-php/issues/35
101:                 $options[CURLOPT_HTTPHEADER][] = 'Content-Length: 0';
102:             } else {
103:                 $options[CURLOPT_POSTFIELDS] = json_encode($reqBody);
104:             }
105:         }
106: 
107:         $curl->setoptArray($options);
108: 
109:         $result = $curl->exec();
110: 
111:         if ($curl->errno()) {
112:             throw new CurlExecutionException($curl->error());
113:         }
114: 
115:         $info = $curl->getinfo();
116:         $httpStatus = $info['http_code'];
117: 
118:         $responseHeaderSize = $info['header_size'];
119: 
120:         $responseHeaderStr = substr($result, 0, $responseHeaderSize);
121:         $responseHeaders = [];
122:         foreach (explode("\r\n", $responseHeaderStr) as $responseHeader) {
123:             $kv = explode(':', $responseHeader, 2);
124:             if (count($kv) === 2) {
125:                 $responseHeaders[$kv[0]] = trim($kv[1]);
126:             }
127:         }
128: 
129:         $body = substr($result, $responseHeaderSize);
130: 
131:         return new Response($httpStatus, $body, $responseHeaders);
132:     }
133: }
134: 
line-bot-sdk-php API documentation generated by ApiGen