Skip to content
This repository has been archived by the owner on Jul 10, 2022. It is now read-only.

Commit

Permalink
Add Proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
GoneTone committed Dec 11, 2020
1 parent b7d020f commit 2b7fb6e
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 16 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ try {
### 網頁播放電台範例
請看 [examples/webpage/play_radio.php](examples/webpage/play_radio.php)

## 代理 (Proxy)
如果運行此程式的伺服器不在台灣,請設定台灣的 Proxy 伺服器,否則取得的串流網址會驗證失敗 (HTTP 403 Forbidden),但如果播放端 IP 在國外一樣會被阻擋就是了。
```php
use GoneTone\HiNetHichannel;
use GoneTone\Proxy;

require_once(dirname(__FILE__) . "/vendor/autoload.php");

/* 連線到 Proxy 伺服器 */
$proxy = new Proxy("主機名", 3128, "http");
//$proxy->login("帳號", "密碼"); //如果 Proxy 伺服器需要驗證,請調用這登入

$hichannel = new HiNetHichannel("電台名稱", $proxy);
```

## 補充
如果需要取得新資料,必須再次調用 `$hichannel->loadApi()` 才會取得最新資料。
```php
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"require": {
"php": ">=7.2",
"chrisyue/php-m3u8": "^3.4",
"guzzlehttp/guzzle": "^7.2"
"guzzlehttp/guzzle": "^7.2",
"ext-json": "*"
},
"autoload": {
"psr-4": {
Expand Down
9 changes: 9 additions & 0 deletions examples/cli/all_demo.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
<?php

use GoneTone\HiNetHichannel;
//use GoneTone\Proxy;

require_once(dirname(dirname(dirname(__FILE__))) . "/vendor/autoload.php");

/*
* 連線到 Proxy 伺服器
* 如果運行此程式的伺服器不在台灣,請設定台灣的 Proxy 伺服器,否則取得的串流網址會驗證失敗 (HTTP 403 Forbidden),但如果播放端 IP 在國外一樣會被阻擋就是了。
*/
//$proxy = new Proxy("主機名", 3128, "http");
//$proxy->login("帳號", "密碼"); //如果 Proxy 伺服器需要驗證,請調用這登入

$hichannel = new HiNetHichannel("KISS RADIO 大眾廣播電台"); //請輸入完整頻道名稱
//$hichannel = new HiNetHichannel("KISS RADIO 大眾廣播電台", $proxy); //Proxy

try {
/* 加載 HiNet hichannel API */
Expand Down
9 changes: 9 additions & 0 deletions examples/webpage/play_radio.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
<?php

use GoneTone\HiNetHichannel;
//use GoneTone\Proxy;

require_once(dirname(dirname(dirname(__FILE__))) . "/vendor/autoload.php");

/*
* 連線到 Proxy 伺服器
* 如果運行此程式的伺服器不在台灣,請設定台灣的 Proxy 伺服器,否則取得的串流網址會驗證失敗 (HTTP 403 Forbidden),但如果播放端 IP 在國外一樣會被阻擋就是了。
*/
//$proxy = new Proxy("主機名", 3128, "http");
//$proxy->login("帳號", "密碼"); //如果 Proxy 伺服器需要驗證,請調用這登入

$hichannel = new HiNetHichannel("KISS RADIO 大眾廣播電台"); //請輸入完整頻道名稱
//$hichannel = new HiNetHichannel("KISS RADIO 大眾廣播電台", $proxy); //Proxy

$playUrl = null;
$title = "無法取得";
Expand Down
9 changes: 5 additions & 4 deletions src/GoneTone/HiNetHichannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ class HiNetHichannel extends Request
* HiNetHichannel constructor.
*
* @param string $hichannelChannelName Hichannel 頻道名稱
* @param ?Proxy $proxy 代理伺服器 (預設不使用代理)
*/
public function __construct(string $hichannelChannelName) {
parent::__construct();
public function __construct(string $hichannelChannelName, ?Proxy $proxy = null) {
parent::__construct($proxy);

$this->_hichannelChannelName = $hichannelChannelName;

$this->_hichannel = new HichannelApi($hichannelChannelName);
$this->_hichannel = new HichannelApi($hichannelChannelName, $proxy);
$this->_loadApi = false;
}

Expand All @@ -43,7 +44,7 @@ public function loadApi() {
if ($api) {
$this->_loadApi = true;
} else {
throw new Exception("Hichannel 頻道名稱「" . $this->_hichannelChannelName . "廣播電台找不到或資料取得失敗");
throw new Exception("Hichannel 頻道名稱「" . $this->_hichannelChannelName . "廣播電台 API 加載失敗");
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/GoneTone/HichannelApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ class HichannelApi extends Request
* HichannelApi constructor.
*
* @param string $hichannelChannelName Hichannel 頻道名稱
* @param ?Proxy $proxy 代理伺服器 (預設不使用代理)
*/
public function __construct(string $hichannelChannelName) {
parent::__construct();
public function __construct(string $hichannelChannelName, ?Proxy $proxy = null) {
parent::__construct($proxy);

$this->_hichannelChannelName = $hichannelChannelName;
}
Expand Down
61 changes: 61 additions & 0 deletions src/GoneTone/Proxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Copyright 2020 GoneTone
*
* HiNet hichannel 台灣電台
* https://github.com/GoneToneStudio/php-hinet-hichannel-taiwan-radio
*
* @author 張文相 Zhang Wenxiang (旋風之音 GoneTone) <https://blog.reh.tw>
* @license MIT <https://github.com/GoneToneStudio/php-hinet-hichannel-taiwan-radio/blob/master/LICENSE>
*
* Proxy
*/

namespace GoneTone;

class Proxy
{
protected $_host;
protected $_port;
protected $_protocol;
protected $_username;
protected $_password;

/**
* Proxy constructor.
*
* @param string $host 主機名
* @param int $port 端口 (預設 3128)
* @param string $protocol 協定 (預設 http)
*/
public function __construct(string $host, int $port = 3128, string $protocol = "http") {
$this->_host = $host;
$this->_port = $port;
$this->_protocol = $protocol;
}

/**
* 登入
*
* @param string $username 帳號
* @param string $password 密碼
*/
public function login(string $username, string $password) {
$this->_username = $username;
$this->_password = $password;
}

/**
* @return string
*/
public function __toString(): string {
$url = $this->_host . ":" . $this->_port;

$proxy = $this->_protocol . "://" . $url;
if ($this->_username && $this->_password) {
$proxy = $this->_protocol . "://" . $this->_username . ":" . $this->_password . "@" . $url;
}

return $proxy;
}
}
31 changes: 22 additions & 9 deletions src/GoneTone/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,32 @@ class Request
protected $_client;

protected $_hichannelUrl = "https://hichannel.hinet.net";
protected $_userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36";

/**
* Request constructor.
*
* @param ?Proxy $proxy 代理伺服器 (預設不使用代理)
*/
protected function __construct() {
$userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36";
$this->_client = new Client([
"verify" => false,
"headers" => [
"User-Agent" => $userAgent,
"Referer" => "https://hichannel.hinet.net/radio/index.do"
]
]);
protected function __construct(?Proxy $proxy = null) {
if ($proxy) {
$this->_client = new Client([
"verify" => false,
"headers" => [
"User-Agent" => $this->_userAgent,
"Referer" => "https://hichannel.hinet.net/radio/index.do"
],
"proxy" => $proxy
]);
} else {
$this->_client = new Client([
"verify" => false,
"headers" => [
"User-Agent" => $this->_userAgent,
"Referer" => "https://hichannel.hinet.net/radio/index.do"
]
]);
}
}

/**
Expand Down

0 comments on commit 2b7fb6e

Please sign in to comment.