update curl;

require https://github.com/ledccn/IYUUBittorrentClient
This commit is contained in:
david 2021-01-12 14:14:14 +08:00
parent 87d9f81bad
commit e66d632ba3
12 changed files with 227 additions and 1341 deletions

View File

@ -13,7 +13,7 @@ use IYUU\Library\Table;
class AutoReseed
{
// 版本号
const VER = '1.10.19';
const VER = '1.10.20';
// RPC连接
private static $links = [];
// 客户端配置
@ -298,7 +298,7 @@ class AutoReseed
continue;
}
echo "正在从下载器 clients_".$k." 获取种子哈希……".PHP_EOL;
$hashArray = self::$links[$k]['rpc']->getList();
$hashArray = self::$links[$k]['rpc']->all();
if (empty($hashArray)) {
continue;
}
@ -618,7 +618,7 @@ class AutoReseed
}
echo "正在从下载器 clients_".$k." 获取种子哈希……".PHP_EOL;
$move = []; // 客户端做种列表 传址
$hashArray = self::$links[$k]['rpc']->getList($move);
$hashArray = self::$links[$k]['rpc']->all($move);
if (empty($hashArray)) {
// 失败
continue;

View File

@ -1,66 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: David <367013672@qq.com>
* Date: 2020-2-14
* Time: 21:31:49
*/
namespace IYUU\Client;
abstract class AbstractClient
{
/**
* 公共方法:创建客户端实例
*/
public static function create($config = array())
{
$type = $config['type'];
$host = $config['host'];
$username = $config['username'];
$password = $config['password'];
$file = __DIR__ . DIRECTORY_SEPARATOR . $type . DIRECTORY_SEPARATOR . $type .'.php';
if (!is_file($file)) {
die($file.' 文件不存在');
}
$className = "\IYUU\Client\\" . $type . "\\" . $type;
if (class_exists($className)) {
echo $type." 客户端正在实例化!".PHP_EOL;
return new $className($host, $username, $password);
} else {
die($className.' 客户端不存在');
}
}
/**
* 查询Bittorrent客户端状态
*
* @return string
*/
abstract public function status();
/**
* 获取种子列表
* @return array(
'hash' => string json,
'sha1' => string,
'hashString '=> array
)
*/
abstract public function getList(&$move = array());
/**
* 添加种子连接
*/
abstract public function add($torrent_url, $save_path = '', $extra_options = array());
/**
* 添加种子原数据
*/
abstract public function add_metainfo($torrent_url, $save_path = '', $extra_options = array());
/**
* 删除种子
*/
abstract public function delete($hash, $deleteFiles = false);
}

View File

@ -1,351 +0,0 @@
<?php
namespace IYUU\Client\qBittorrent;
use Curl\Curl;
use IYUU\Client\AbstractClient;
/**
* https://github.com/qbittorrent/qBittorrent/wiki/Web-API-Documentation
*/
class qBittorrent extends AbstractClient
{
private $debug;
private $url;
private $api_version;
private $curl;
protected $delimiter;
private $endpoints = [
'login' => [
'1' => '/login',
'2' => '/api/v2/auth/login'
],
'app_version' => [
'1' => '/version/qbittorrent',
'2' => '/api/v2/app/version'
],
'api_version' => [
'1' => '/version/api',
'2' => '/api/v2/app/webapiVersion'
],
'build_info' => [
'1' => null,
'2' => '/api/v2/app/buildInfo'
],
'preferences' => [
'1' => null,
'2' => '/api/v2/app/preferences'
],
'setPreferences' => [
'1' => null,
'2' => '/api/v2/app/setPreferences'
],
'defaultSavePath' => [
'1' => null,
'2' => '/api/v2/app/defaultSavePath'
],
'torrent_list' => [
'1' => null,
'2' => '/api/v2/torrents/info'
],
'torrent_add' => [
'1' => null,
'2' => '/api/v2/torrents/add'
],
'torrent_delete' => [
'1' => null,
'2' => '/api/v2/torrents/delete'
],
'torrent_pause' => [
'1' => null,
'2' => '/api/v2/torrents/pause'
],
'torrent_resume' => [
'1' => null,
'2' => '/api/v2/torrents/resume'
],
'set_torrent_location' => [
'1' => null,
'2' => '/api/v2/torrents/setLocation'
],
'maindata' => [
'1' => null,
'2' => '/api/v2/sync/maindata'
]
];
public function __construct($url='', $username='', $password='', $api_version = 2, $debug = false)
{
$this->debug = $debug;
$this->url = rtrim($url, '/');
$this->username = $username;
$this->password = $password;
$this->api_version = $api_version;
$this->curl = new Curl();
$this->curl->setOpt(CURLOPT_SSL_VERIFYPEER, false); // 禁止验证证书
$this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, 2); // 不检查证书
$this->curl->setOpt(CURLOPT_CONNECTTIMEOUT, 60); // 超时
$this->curl->setOpt(CURLOPT_TIMEOUT, 600); // 超时
// Authenticate and get cookie, else throw exception
if (!$this->authenticate()) {
throw new \Exception("qBittorrent Unable to authenticate with Web Api.");
}
}
public function appVersion()
{
return $this->getData('app_version');
}
public function apiVersion()
{
return $this->getData('api_version');
}
public function buildInfo()
{
return $this->getData('build_info');
}
public function preferences($data = null)
{
if (!empty($data)) {
return $this->postData('setPreferences', ['json' => json_encode($data)]);
}
return $this->getData('preferences');
}
public function torrentList()
{
return $this->getData('torrent_list');
}
/**
* @param array $extra_options
array(
'urls' => '',
'savepath' => '',
'cookie' => '',
'category' => '',
'skip_checking' => true,
'paused' => true,
'root_folder' => true,
)
* @return array
*/
public function add($torrent_url, $save_path = '', $extra_options = array())
{
if (!empty($save_path)) {
$extra_options['savepath'] = $save_path;
}
$extra_options['urls'] = $torrent_url;
#$extra_options['skip_checking'] = 'true'; //跳校验
// 关键 上传文件流 multipart/form-data【严格按照api文档编写】
$post_data = $this->buildUrls($extra_options);
#p($post_data);
// 设置请求头
$this->curl->setHeader('Content-Type', 'multipart/form-data; boundary='.$this->delimiter);
$this->curl->setHeader('Content-Length', strlen($post_data));
return $this->postData('torrent_add', $post_data);
}
public function add_metainfo($torrent_metainfo, $save_path = '', $extra_options = array())
{
if (!empty($save_path)) {
$extra_options['savepath'] = $save_path;
}
$extra_options['torrents'] = $torrent_metainfo;
#$extra_options['skip_checking'] = 'true'; //跳校验
// 关键 上传文件流 multipart/form-data【严格按照api文档编写】
$post_data = $this->buildData($extra_options);
// 设置请求头
$this->curl->setHeader('Content-Type', 'multipart/form-data; boundary='.$this->delimiter);
$this->curl->setHeader('Content-Length', strlen($post_data));
return $this->postData('torrent_add', $post_data);
}
public function torrentDeleteAll($deleteFiles = false)
{
$torrents = json_decode($this->torrentList());
$response = '';
foreach ($torrents as $torrent) {
$response .= $this->delete($torrent->hash, $deleteFiles);
}
return $response;
}
public function torrentPause($hash)
{
return $this->postData('torrent_pause', ['hashes' => $hash]);
}
public function torrentResume($hash)
{
return $this->postData('torrent_resume', ['hashes' => $hash]);
}
public function setTorrentLocation($hash, $location)
{
return $this->postData('set_torrent_location', ['hashes' => $hash, 'location' => $location]);
}
private function getData($endpoint)
{
$this->curl->get($this->url . $this->endpoints[$endpoint][$this->api_version]);
if ($this->debug) {
var_dump($this->curl->request_headers);
var_dump($this->curl->response_headers);
}
if ($this->curl->error) {
return $this->errorMessage();
}
return $this->curl->response;
}
private function postData($endpoint, $data)
{
$this->curl->post($this->url . $this->endpoints[$endpoint][$this->api_version], $data);
if ($this->debug) {
var_dump($this->curl->request_headers);
var_dump($this->curl->response_headers);
}
if ($this->curl->error) {
return $this->errorMessage();
}
return $this->curl->response;
}
private function authenticate()
{
$this->curl->post($this->url . $this->endpoints['login'][$this->api_version], [
'username' => $this->username,
'password' => $this->password
]);
if ($this->debug) {
var_dump($this->curl->request_headers);
var_dump($this->curl->response_headers);
}
// Find authentication cookie and set in curl connection
foreach ($this->curl->response_headers as $header) {
if (preg_match('/SID=(\S[^;]+)/', $header, $matches)) {
$qb415 = '; QB_'.$matches[0]; // 兼容qBittorrent v4.1.5[小钢炮等]
$this->curl->setHeader('Cookie', $matches[0].$qb415);
return true;
}
};
return false;
}
private function errorMessage()
{
return 'Curl Error Code: ' . $this->curl->error_code . ' (' . $this->curl->response . ')';
}
/**
* 拼接种子urls multipart/form-data
* https://github.com/qbittorrent/qBittorrent/wiki/Web-API-Documentation#add-new-torrent
*/
private function buildUrls($param)
{
$this->delimiter = uniqid();
$eol = "\r\n";
$data = '';
// 拼接文件流
foreach ($param as $name => $content) {
$data .= "--" . $this->delimiter . $eol;
$data .= 'Content-Disposition: form-data; name="' .$name. '"' . $eol . $eol;
$data .= $content . $eol;
}
$data .= "--" . $this->delimiter . "--" . $eol;
return $data;
}
/**
* 拼接种子上传文件流 multipart/form-data
* https://github.com/qbittorrent/qBittorrent/wiki/Web-API-Documentation#add-new-torrent
*/
private function buildData($param)
{
$this->delimiter = uniqid();
$eol = "\r\n";
$data = '';
// 拼接文件流
$data .= "--" . $this->delimiter . $eol;
$data .= 'Content-Disposition: form-data; name="' .$param['name']. '"; filename="'.$param['filename'].'"' . $eol;
$data .= 'Content-Type: application/x-bittorrent' . $eol . $eol;
$data .= $param['torrents'] . $eol;
unset($param['name']);
unset($param['filename']);
unset($param['torrents']);
if (!empty($param)) {
foreach ($param as $name => $content) {
$data .= "--" . $this->delimiter . $eol;
$data .= 'Content-Disposition: form-data; name="' . $name . '"' . $eol . $eol;
$data .= $content . $eol;
}
}
$data .= "--" . $this->delimiter . "--" . $eol;
return $data;
}
/**
* 抽象方法,子类实现
*/
public function status()
{
return $this->appVersion();
}
/**
* 抽象方法,子类实现
*/
public function getList(&$move = array())
{
$result = $this->getData('torrent_list');
$res = json_decode($result, true);
if (empty($res)) {
echo "获取种子列表失败可能qBittorrent暂时无响应请稍后重试".PHP_EOL;
return array();
}
// 过滤,只保留正常做种
$res = array_filter($res, function ($v) {
if (isset($v['state']) && in_array($v['state'], array('uploading','stalledUP','pausedUP','queuedUP','checkingUP','forcedUP'))) {
return true;
}
return false;
}, ARRAY_FILTER_USE_BOTH);
if (empty($res)) {
echo "未获取到正常做种数据,请多保种,然后重试!".PHP_EOL;
return array();
}
// 提取数组hashString
$info_hash = array_column($res, 'hash');
// 升序排序
sort($info_hash);
$json = json_encode($info_hash, JSON_UNESCAPED_UNICODE);
// 去重 应该从文件读入,防止重复提交
$sha1 = sha1($json);
// 组装返回数据
$hashArray['hash'] = $json;
$hashArray['sha1'] = $sha1;
// 变换数组hashString为键
$hashArray['hashString'] = array_column($res, "save_path", 'hash');
return $hashArray;
}
/**
* 抽象方法,子类实现
*/
public function delete($hash='', $deleteFiles = false)
{
return $this->postData('torrent_delete', ['hashes' => $hash, 'deleteFiles' => $deleteFiles ? 'true':'false']);
}
}

View File

@ -1,48 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 1/17/2020
* Time: 2020
*/
namespace IYUU\Client\transmission;
/**
* This is the type of exception the TransmissionRPC class will throw
*/
class TransmissionRPCException extends \Exception
{
/**
* Exception: Invalid arguments
*/
const E_INVALIDARG = -1;
/**
* Exception: Invalid Session-Id
*/
const E_SESSIONID = -2;
/**
* Exception: Error while connecting
*/
const E_CONNECTION = -3;
/**
* Exception: Error 401 returned, unauthorized
*/
const E_AUTHENTICATION = -4;
/**
* Exception constructor
*/
public function __construct($message = null, $code = 0, \Exception $previous = null)
{
// PHP version 5.3.0 and above support Exception linking
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
parent::__construct($message, $code, $previous);
} else {
parent::__construct($message, $code);
}
}
}

View File

@ -1,763 +0,0 @@
<?php
/**
* Transmission bittorrent client/daemon RPC communication class
* Copyright (C) 2010 Johan Adriaans <johan.adriaans@gmail.com>,
* Bryce Chidester <bryce@cobryce.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* PHP version specific information
* version_compare() (PHP 4 >= 4.1.0, PHP 5)
* ctype_digit() (PHP 4 >= 4.0.4, PHP 5)
* stream_context_create (PHP 4 >= 4.3.0, PHP 5)
* PHP Class support (PHP 5) (PHP 4 might work, untested)
*/
namespace IYUU\Client\transmission;
use IYUU\Client\AbstractClient;
/**
* A friendly little version check...
*/
if (version_compare(PHP_VERSION, '5.2.10', '<')) {
die("The TransmissionRPC class requires PHP version 5.2.10 or above." . PHP_EOL);
}
/**
* Transmission bittorrent client/daemon RPC communication class
*
* Usage example:
* <code>
* $rpc = new TransmissionRPC($rpc_url);
* $result = $rpc->add_file( $url_or_path_to_torrent, $target_folder );
* </code>
*
*/
class transmission extends AbstractClient
{
/**
* User agent used in all http communication
*/
const HTTP_UA = 'TransmissionRPC for PHP/0.3';
/**
* The URL to the bittorent client you want to communicate with
* the port (default: 9091) can be set in you Transmission preferences
* @var string
*/
public $url = '';
/**
* If your Transmission RPC requires authentication, supply username here
* @var string
*/
public $username = '';
/**
* If your Transmission RPC requires authentication, supply password here
* @var string
*/
public $password = '';
/**
* Print debugging information, default is off
* @var bool
*/
public $debug = false;
/**
* Transmission RPC version
* @var int
*/
protected $rpc_version = 0;
/**
* Transmission uses a session id to prevent CSRF attacks
* @var string
*/
protected $session_id = '';
/**
* Default values for stream context
* @var array
*/
private $default_context_opts = array(
'http' => array(
'user_agent' => self::HTTP_UA,
'timeout' => '60', // Don't want to be too slow
'ignore_errors' => true, // Leave the error parsing/handling to the code
),
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
)
);
/**
* Constants for torrent status
*/
const TR_STATUS_STOPPED = 0;
const TR_STATUS_CHECK_WAIT = 1;
const TR_STATUS_CHECK = 2;
const TR_STATUS_DOWNLOAD_WAIT = 3;
const TR_STATUS_DOWNLOAD = 4;
const TR_STATUS_SEED_WAIT = 5;
const TR_STATUS_SEED = 6;
const RPC_LT_14_TR_STATUS_CHECK_WAIT = 1;
const RPC_LT_14_TR_STATUS_CHECK = 2;
const RPC_LT_14_TR_STATUS_DOWNLOAD = 4;
const RPC_LT_14_TR_STATUS_SEED = 8;
const RPC_LT_14_TR_STATUS_STOPPED = 16;
/**
* Takes the connection parameters
*
* @param string $url
* @param string $username
* @param string $password
*/
public function __construct($url = 'http://127.0.0.1:9091/transmission/rpc', $username = null, $password = null)
{
$this->url = rtrim($url, '/');
$this->username = $username;
$this->password = $password;
}
/**
* Start one or more torrents
*
* @param int|array ids A list of transmission torrent ids
* @return mixed
* @throws TransmissionRPCException
*/
public function start($ids)
{
if (!is_array($ids)) {
$ids = array($ids);
}
$request = array("ids" => $ids);
return $this->request("torrent-start", $request);
}
/**
* Stop one or more torrents
*
* @param int|array ids A list of transmission torrent ids
* @return mixed
* @throws TransmissionRPCException
*/
public function stop($ids)
{
if (!is_array($ids)) {
$ids = array($ids);
}
$request = array("ids" => $ids);
return $this->request("torrent-stop", $request);
}
/**
* Reannounce one or more torrents
*
* @param int|array ids A list of transmission torrent ids
* @return mixed
* @throws TransmissionRPCException
*/
public function reannounce($ids)
{
if (!is_array($ids)) {
$ids = array($ids);
}
$request = array("ids" => $ids);
return $this->request("torrent-reannounce", $request);
}
/**
* Verify one or more torrents
*
* @param int|array ids A list of transmission torrent ids
* @return mixed
* @throws TransmissionRPCException
*/
public function verify($ids)
{
if (!is_array($ids)) {
$ids = array($ids);
}
$request = array("ids" => $ids);
return $this->request("torrent-verify", $request);
}
/**
* Get information on torrents in transmission, if the ids parameter is
* empty all torrents will be returned. The fields array can be used to return certain
* fields. Default fields are: "id", "name", "status", "doneDate", "haveValid", "totalSize".
* See https://github.com/transmission/transmission/blob/2.9x/extras/rpc-spec.txt for available fields
*
* @param array fields An array of return fields
* @param int|array ids A list of transmission torrent ids
*
* Request:
* {
* "arguments": {
* "fields": [ "id", "name", "totalSize" ],
* "ids": [ 7, 10 ]
* },
* "method": "torrent-get",
* "tag": 39693
* }
*
* Response:
* {
* "arguments": {
* "torrents": [
* {
* "id": 10,
* "name": "Fedora x86_64 DVD",
* "totalSize": 34983493932,
* },
* {
* "id": 7,
* "name": "Ubuntu x86_64 DVD",
* "totalSize", 9923890123,
* }
* ]
* },
* "result": "success",
* "tag": 39693
* }
* @return mixed
* @throws TransmissionRPCException
*/
public function get($ids = array(), $fields = array())
{
if (!is_array($ids)) {
$ids = array($ids);
} // Convert $ids to an array if only a single id was passed
if (count($fields) == 0) {
$fields = array("id", "name", "status", "doneDate", "haveValid", "totalSize");
} // Defaults
$request = array(
"fields" => $fields,
"ids" => $ids
);
return $this->request("torrent-get", $request);
}
/**
* Set properties on one or more torrents, available fields are:
* "bandwidthPriority" | number this torrent's bandwidth tr_priority_t
* "downloadLimit" | number maximum download speed (in K/s)
* "downloadLimited" | boolean true if "downloadLimit" is honored
* "files-wanted" | array indices of file(s) to download
* "files-unwanted" | array indices of file(s) to not download
* "honorsSessionLimits" | boolean true if session upload limits are honored
* "ids" | array torrent list, as described in 3.1
* "location" | string new location of the torrent's content
* "peer-limit" | number maximum number of peers
* "priority-high" | array indices of high-priority file(s)
* "priority-low" | array indices of low-priority file(s)
* "priority-normal" | array indices of normal-priority file(s)
* "seedRatioLimit" | double session seeding ratio
* "seedRatioMode" | number which ratio to use. See tr_ratiolimit
* "uploadLimit" | number maximum upload speed (in K/s)
* "uploadLimited" | boolean true if "uploadLimit" is honored
* See https://github.com/transmission/transmission/blob/2.9x/extras/rpc-spec.txt for more information
*
* @param array arguments An associative array of arguments to set
* @param int|array ids A list of transmission torrent ids
* @return mixed
* @throws TransmissionRPCException
*/
public function set($ids = array(), $arguments = array())
{
// See https://github.com/transmission/transmission/blob/2.9x/extras/rpc-spec.txt for available fields
if (!is_array($ids)) {
$ids = array($ids);
} // Convert $ids to an array if only a single id was passed
if (!isset($arguments['ids'])) {
$arguments['ids'] = $ids;
} // Any $ids given in $arguments overrides the method parameter
return $this->request("torrent-set", $arguments);
}
/**
* Add a new torrent
*
* Available extra options:
* key | value type & description
* ---------------------+-------------------------------------------------
* "download-dir" | string path to download the torrent to
* "filename" | string filename or URL of the .torrent file
* "metainfo" | string base64-encoded .torrent content
* "paused" | boolean if true, don't start the torrent
* "peer-limit" | number maximum number of peers
* "bandwidthPriority" | number torrent's bandwidth tr_priority_t
* "files-wanted" | array indices of file(s) to download
* "files-unwanted" | array indices of file(s) to not download
* "priority-high" | array indices of high-priority file(s)
* "priority-low" | array indices of low-priority file(s)
* "priority-normal" | array indices of normal-priority file(s)
*
* Either "filename" OR "metainfo" MUST be included.
* All other arguments are optional.
*
* @param string $torrent_location The URL or path to the torrent file
* @param string $save_path Folder to save torrent in
* @param array $extra_options Optional extra torrent options
* @return mixed
* @throws TransmissionRPCException
*/
public function add_file($torrent_location, $save_path = '', $extra_options = array())
{
if (!empty($save_path)) {
$extra_options['download-dir'] = $save_path;
}
$extra_options['filename'] = $torrent_location;
return $this->request("torrent-add", $extra_options);
}
/* Add a new torrent using a file path or a URL (For backwards compatibility)
* @param torrent_location The URL or path to the torrent file
* @param save_path Folder to save torrent in
* @param extra options Optional extra torrent options
*/
public function add($torrent_location, $save_path = '', $extra_options = array())
{
return $this->add_file($torrent_location, $save_path, $extra_options);
}
/**
* Add a torrent using the raw torrent data
*
* @param string $torrent_metainfo The raw, unencoded contents (metainfo) of a torrent
* @param string $save_path Folder to save torrent in
* @param array $extra_options Optional extra torrent options
* @return mixed
* @throws TransmissionRPCException
*/
public function add_metainfo($torrent_metainfo, $save_path = '', $extra_options = array())
{
if (!empty($save_path)) {
$extra_options['download-dir'] = $save_path;
}
$extra_options['metainfo'] = base64_encode($torrent_metainfo);
return $this->request("torrent-add", $extra_options);
}
/**
* Remove torrent from transmission
*
* @param bool delete_local_data Also remove local data?
* @param int|array ids A list of transmission torrent ids
* @return mixed
* @throws TransmissionRPCException
*/
public function delete($ids, $delete_local_data = false)
{
if (!is_array($ids)) {
$ids = array($ids);
} // Convert $ids to an array if only a single id was passed
$request = array(
"ids" => $ids,
"delete-local-data" => $delete_local_data
);
return $this->request("torrent-remove", $request);
}
/**
* Move local storage location
*
* @param int|array ids A list of transmission torrent ids
* @param string target_location The new storage location
* @param string move_existing_data Move existing data or scan new location for available data
* @return mixed
* @throws TransmissionRPCException
*/
public function move($ids, $target_location, $move_existing_data = true)
{
if (!is_array($ids)) {
$ids = array($ids);
} // Convert $ids to an array if only a single id was passed
$request = array(
"ids" => $ids,
"location" => $target_location,
"move" => $move_existing_data
);
return $this->request("torrent-set-location", $request);
}
/**
* 3.7. Renaming a Torrent's Path
*
* Method name: "torrent-rename-path"
*
* For more information on the use of this function, see the transmission.h
* documentation of tr_torrentRenamePath(). In particular, note that if this
* call succeeds you'll want to update the torrent's "files" and "name" field
* with torrent-get.
*
* Request arguments:
*
* string | value type & description
* ---------------------------------+-------------------------------------------------
* "ids" | array the torrent torrent list, as described in 3.1
* | (must only be 1 torrent)
* "path" | string the path to the file or folder that will be renamed
* "name" | string the file or folder's new name
* Response arguments: "path", "name", and "id", holding the torrent ID integer
*
* @param int|array ids A 1-element list of transmission torrent ids
* @param string path The path to the file or folder that will be renamed
* @param string name The file or folder's new name
* @return mixed
* @throws TransmissionRPCException
*/
public function rename($ids, $path, $name)
{
if (!is_array($ids)) {
$ids = array($ids);
} // Convert $id to an array if only a single id was passed
if (count($ids) !== 1) {
throw new TransmissionRPCException('A single id is accepted', TransmissionRPCException::E_INVALIDARG);
}
$request = array(
"ids" => $ids,
"path" => $path,
"name" => $name
);
return $this->request("torrent-rename-path", $request);
}
/**
* Retrieve session statistics
*
* @returns array of statistics
*/
public function sstats()
{
return $this->request("session-stats", array());
}
/**
* Retrieve all session variables
*
* @returns array of session information
*/
public function sget()
{
return $this->request("session-get", array());
}
/**
* Set session variable(s)
*
* @param array of session variables to set
* @return mixed
* @throws TransmissionRPCException
*/
public function sset($arguments)
{
return $this->request("session-set", $arguments);
}
/**
* Return the interpretation of the torrent status
*
* @param int The integer "torrent status"
* @returns string The translated meaning
* @return string
*/
public function getStatusString($intstatus)
{
if ($this->rpc_version < 14) {
if ($intstatus == self::RPC_LT_14_TR_STATUS_CHECK_WAIT) {
return "Waiting to verify local files";
}
if ($intstatus == self::RPC_LT_14_TR_STATUS_CHECK) {
return "Verifying local files";
}
if ($intstatus == self::RPC_LT_14_TR_STATUS_DOWNLOAD) {
return "Downloading";
}
if ($intstatus == self::RPC_LT_14_TR_STATUS_SEED) {
return "Seeding";
}
if ($intstatus == self::RPC_LT_14_TR_STATUS_STOPPED) {
return "Stopped";
}
} else {
if ($intstatus == self::TR_STATUS_CHECK_WAIT) {
return "Waiting to verify local files";
}
if ($intstatus == self::TR_STATUS_CHECK) {
return "Verifying local files";
}
if ($intstatus == self::TR_STATUS_DOWNLOAD) {
return "Downloading";
}
if ($intstatus == self::TR_STATUS_SEED) {
return "Seeding";
}
if ($intstatus == self::TR_STATUS_STOPPED) {
return "Stopped";
}
if ($intstatus == self::TR_STATUS_SEED_WAIT) {
return "Queued for seeding";
}
if ($intstatus == self::TR_STATUS_DOWNLOAD_WAIT) {
return "Queued for download";
}
}
return "Unknown";
}
/**
* Here be dragons (Internal methods)
*/
/**
* Clean up the request array. Removes any empty fields from the request
*
* @param array array The request associative array to clean
* @returns array The cleaned array
* @return array|null
*/
protected function cleanRequestData($array)
{
if (!is_array($array) || count($array) == 0) {
return null;
} // Nothing to clean
setlocale(LC_NUMERIC, 'en_US.utf8'); // Override the locale - if the system locale is wrong, then 12.34 will encode as 12,34 which is invalid JSON
foreach ($array as $index => $value) {
if (is_object($value)) {
$array[$index] = $value->toArray();
} // Convert objects to arrays so they can be JSON encoded
if (is_array($value)) {
$array[$index] = $this->cleanRequestData($value);
} // Recursion
if (empty($value) && $value !== 0) { // Remove empty members
unset($array[$index]);
continue; // Skip the rest of the tests - they may re-add the element.
}
if (is_numeric($value)) {
$array[$index] = $value + 0;
} // Force type-casting for proper JSON encoding (+0 is a cheap way to maintain int/float/etc)
if (is_bool($value)) {
$array[$index] = ($value ? 1 : 0);
} // Store boolean values as 0 or 1
if (is_string($value)) {
$type = mb_detect_encoding($value, "auto");
if ($type !== 'UTF-8') {
$array[$index] = mb_convert_encoding($value, "UTF-8");
//utf8_encode( $value ); // Make sure all data is UTF-8 encoded for Transmission
}
}
}
return $array;
}
/**
* 执行 rpc 请求
*
* @param string $method 请求类型/方法, 详见 $this->allowMethods
* @param array $arguments 附加参数, 可选
* @return array
* @throws TransmissionRPCException
*/
protected function request($method, $arguments = array())
{
// Check the parameters
if (!is_scalar($method)) {
throw new TransmissionRPCException('Method name has no scalar value', TransmissionRPCException::E_INVALIDARG);
}
if (!is_array($arguments)) {
throw new TransmissionRPCException('Arguments must be given as array', TransmissionRPCException::E_INVALIDARG);
}
$arguments = $this->cleanRequestData($arguments); // Sanitize input
// Grab the X-Transmission-Session-Id if we don't have it already
if (!$this->session_id) {
if (!$this->GetSessionID()) {
throw new TransmissionRPCException('Unable to acquire X-Transmission-Session-Id', TransmissionRPCException::E_SESSIONID);
}
}
$data = array(
'method' => $method,
'arguments' => $arguments
);
$header = array(
'Content-Type: application/json',
'Authorization: Basic ' . base64_encode(sprintf("%s:%s", $this->username, $this->password)),
'X-Transmission-Session-Id: ' . $this->session_id
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
if (stripos($this->url, 'https://') === 0) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 禁止验证证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 不检查证书
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $this->username . ':' . $this->password);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
$content = curl_exec($ch);
curl_close($ch);
if (!$content) {
$content = array('result' => 'failed');
}
return json_decode($content, true);
}
/**
* Performs an empty GET on the Transmission RPC to get the X-Transmission-Session-Id
* and store it in $this->session_id
*
* @return string
* @throws TransmissionRPCException
*/
public function GetSessionID()
{
if (!$this->url) {
throw new TransmissionRPCException("Class must be initialized before GetSessionID() can be called.", TransmissionRPCException::E_INVALIDARG);
}
// Setup the context
$contextopts = $this->default_context_opts; // Start with the defaults
// Make sure it's blank/empty (reset)
$this->session_id = null;
// Setup authentication (if provided)
if ($this->username && $this->password) {
$contextopts['http']['header'] = sprintf("Authorization: Basic %s\r\n", base64_encode($this->username . ':' . $this->password));
}
if ($this->debug) {
echo "TRANSMISSIONRPC_DEBUG:: GetSessionID():: Stream context created with options:" .
PHP_EOL . print_r($contextopts, true);
}
$context = stream_context_create($contextopts); // Create the context for this request
if (!$fp = @fopen($this->url, 'r', false, $context)) { // Open a filepointer to the data, and use fgets to get the result
throw new TransmissionRPCException('Unable to connect to ' . $this->url, TransmissionRPCException::E_CONNECTION);
}
// Check the response (headers etc)
$stream_meta = stream_get_meta_data($fp);
fclose($fp);
if ($this->debug) {
echo "TRANSMISSIONRPC_DEBUG:: GetSessionID():: Stream meta info: " .
PHP_EOL . print_r($stream_meta, true);
}
if ($stream_meta['timed_out']) {
throw new TransmissionRPCException("Timed out connecting to {$this->url}", TransmissionRPCException::E_CONNECTION);
}
if (substr($stream_meta['wrapper_data'][0], 9, 3) == "401") {
throw new TransmissionRPCException("Invalid username/password.", TransmissionRPCException::E_AUTHENTICATION);
} elseif (substr($stream_meta['wrapper_data'][0], 9, 3) == "409") { // This is what we're hoping to find
// Loop through the returned headers and extract the X-Transmission-Session-Id
foreach ($stream_meta['wrapper_data'] as $header) {
if (strpos($header, 'X-Transmission-Session-Id: ') === 0) {
if ($this->debug) {
echo "TRANSMISSIONRPC_DEBUG:: GetSessionID():: Session-Id header: " .
PHP_EOL . print_r($header, true);
}
$this->session_id = trim(substr($header, 27));
break;
}
}
if (!$this->session_id) { // Didn't find a session_id
throw new TransmissionRPCException("Unable to retrieve X-Transmission-Session-Id", TransmissionRPCException::E_SESSIONID);
}
} else {
throw new TransmissionRPCException("Unexpected response from Transmission RPC: " . $stream_meta['wrapper_data'][0]);
}
return $this->session_id;
}
/**
* 抽象方法,子类实现
*/
public function status()
{
$rs = $this->sstats();
return isset($rs['result']) ? $rs['result'] : 'error';
}
/**
* 抽象方法,子类实现
*/
public function getList(&$move = array())
{
$ids = array();
$fields = array( "id", "status", "name", "hashString", "downloadDir", "torrentFile" );
$res = $this->get($ids, $fields);
if (isset($res['result']) && $res['result'] == 'success') {
// 成功
} else {
// 失败
echo "获取种子列表失败可能transmission暂时无响应请稍后重试".PHP_EOL;
return array();
}
if (empty($res['arguments']['torrents'])) {
echo "未获取到正常做种数据,请多保种,然后重试!".PHP_EOL;
return array();
}
$res = $res['arguments']['torrents'];
// 过滤,只保留正常做种
$res = array_filter($res, function ($v) {
return isset($v['status']) && $v['status']===6;
}, ARRAY_FILTER_USE_BOTH);
if (empty($res)) {
echo "未获取到正常做种数据,请多保种,然后重试!".PHP_EOL;
return array();
}
// 提取数组hashString
$info_hash = array_column($res, 'hashString');
// 升序排序
sort($info_hash);
$json = json_encode($info_hash, JSON_UNESCAPED_UNICODE);
// 去重 应该从文件读入,防止重复提交
$sha1 = sha1($json);
// 组装返回数据
$hashArray['hash'] = $json;
$hashArray['sha1'] = $sha1;
// 变换数组hashString为键
$hashArray['hashString'] = array_column($res, "downloadDir", 'hashString');
$move = array_column($res, null, 'hashString');
return $hashArray;
}
}

View File

@ -13,7 +13,8 @@
"ext-json": "*",
"ext-mbstring": "*",
"ext-curl": "*",
"curl/curl": "^2.2"
"curl/curl": "^2.2",
"ledccn/iyuubittorrentclient": "dev-master"
},
"autoload": {
"psr-4": {

75
composer.lock generated
View File

@ -4,20 +4,20 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "10281f19c929443b7db18d1ab159ec63",
"content-hash": "568a690b54981e283a0cc9ee09f5d696",
"packages": [
{
"name": "curl/curl",
"version": "2.2.0",
"version": "2.3.0",
"source": {
"type": "git",
"url": "https://github.com/php-mod/curl.git",
"reference": "d22086dd2eee5ca02e4c29b9a5bdf3645bfdbbff"
"reference": "3ad560b1fc1bbdf5c7681356ab953fb961f255e5"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-mod/curl/zipball/d22086dd2eee5ca02e4c29b9a5bdf3645bfdbbff",
"reference": "d22086dd2eee5ca02e4c29b9a5bdf3645bfdbbff",
"url": "https://api.github.com/repos/php-mod/curl/zipball/3ad560b1fc1bbdf5c7681356ab953fb961f255e5",
"reference": "3ad560b1fc1bbdf5c7681356ab953fb961f255e5",
"shasum": "",
"mirrors": [
{
@ -45,15 +45,15 @@
"MIT"
],
"authors": [
{
"name": "php-curl-class",
"homepage": "https://github.com/php-curl-class"
},
{
"name": "Hassan Amouhzi",
"email": "hassan@anezi.net",
"homepage": "http://hassan.amouhzi.com"
},
{
"name": "php-curl-class",
"homepage": "https://github.com/php-curl-class"
},
{
"name": "user52",
"homepage": "https://github.com/user52"
@ -65,13 +65,66 @@
"curl",
"dot"
],
"time": "2018-12-04T19:47:03+00:00"
"time": "2020-03-19T20:07:26+00:00"
},
{
"name": "ledccn/iyuubittorrentclient",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/ledccn/IYUUBittorrentClient.git",
"reference": "cb59d3380efd8cab7d277eefbc7e2590845bf805"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/ledccn/IYUUBittorrentClient/zipball/cb59d3380efd8cab7d277eefbc7e2590845bf805",
"reference": "cb59d3380efd8cab7d277eefbc7e2590845bf805",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"curl/curl": "^2.3",
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"php": "^5.6 | ^7.0"
},
"type": "library",
"autoload": {
"psr-4": {
"IYUU\\Client\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"GPL-3.0-or-later"
],
"authors": [
{
"name": "David",
"email": "367013672@qq.com"
}
],
"description": "实现pt客户端管理的代码库",
"homepage": "https://github.com/ledccn/IYUUBittorrentClient",
"keywords": [
"qbittorrent",
"transmission"
],
"time": "2021-01-12T06:07:08+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"stability-flags": {
"ledccn/iyuubittorrentclient": 20
},
"prefer-stable": false,
"prefer-lowest": false,
"platform": {

View File

@ -6,5 +6,6 @@ $vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'IYUU\\Client\\' => array($vendorDir . '/ledccn/iyuubittorrentclient/src'),
'IYUU\\' => array($baseDir . '/app'),
);

View File

@ -13,11 +13,16 @@ class ComposerStaticInitd8553673db02b2a444a853f28e16196e
public static $prefixLengthsPsr4 = array (
'I' =>
array (
'IYUU\\Client\\' => 12,
'IYUU\\' => 5,
),
);
public static $prefixDirsPsr4 = array (
'IYUU\\Client\\' =>
array (
0 => __DIR__ . '/..' . '/ledccn/iyuubittorrentclient/src',
),
'IYUU\\' =>
array (
0 => __DIR__ . '/../..' . '/app',

View File

@ -1,17 +1,17 @@
[
{
"name": "curl/curl",
"version": "2.2.0",
"version_normalized": "2.2.0.0",
"version": "2.3.0",
"version_normalized": "2.3.0.0",
"source": {
"type": "git",
"url": "https://github.com/php-mod/curl.git",
"reference": "d22086dd2eee5ca02e4c29b9a5bdf3645bfdbbff"
"reference": "3ad560b1fc1bbdf5c7681356ab953fb961f255e5"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-mod/curl/zipball/d22086dd2eee5ca02e4c29b9a5bdf3645bfdbbff",
"reference": "d22086dd2eee5ca02e4c29b9a5bdf3645bfdbbff",
"url": "https://api.github.com/repos/php-mod/curl/zipball/3ad560b1fc1bbdf5c7681356ab953fb961f255e5",
"reference": "3ad560b1fc1bbdf5c7681356ab953fb961f255e5",
"shasum": "",
"mirrors": [
{
@ -28,7 +28,7 @@
"phpunit/phpunit": "^5.7",
"squizlabs/php_codesniffer": "~2.1"
},
"time": "2018-12-04T19:47:03+00:00",
"time": "2020-03-19T20:07:26+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
@ -41,15 +41,15 @@
"MIT"
],
"authors": [
{
"name": "php-curl-class",
"homepage": "https://github.com/php-curl-class"
},
{
"name": "Hassan Amouhzi",
"email": "hassan@anezi.net",
"homepage": "http://hassan.amouhzi.com"
},
{
"name": "php-curl-class",
"homepage": "https://github.com/php-curl-class"
},
{
"name": "user52",
"homepage": "https://github.com/user52"
@ -63,18 +63,18 @@
]
},
{
"name": "owner888/phpspider",
"version": "v2.1.6",
"version_normalized": "2.1.6.0",
"name": "ledccn/iyuubittorrentclient",
"version": "dev-master",
"version_normalized": "9999999-dev",
"source": {
"type": "git",
"url": "https://github.com/owner888/phpspider.git",
"reference": "e6021148adec201418c16ba26f39bc013ba5b4d9"
"url": "https://github.com/ledccn/IYUUBittorrentClient.git",
"reference": "cb59d3380efd8cab7d277eefbc7e2590845bf805"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/owner888/phpspider/zipball/e6021148adec201418c16ba26f39bc013ba5b4d9",
"reference": "e6021148adec201418c16ba26f39bc013ba5b4d9",
"url": "https://api.github.com/repos/ledccn/IYUUBittorrentClient/zipball/cb59d3380efd8cab7d277eefbc7e2590845bf805",
"reference": "cb59d3380efd8cab7d277eefbc7e2590845bf805",
"shasum": "",
"mirrors": [
{
@ -84,36 +84,35 @@
]
},
"require": {
"php": ">=5.5.0"
"curl/curl": "^2.3",
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"php": "^5.6 | ^7.0"
},
"suggest": {
"ext-pcntl、ext-redis": "For better performance. "
},
"time": "2018-08-15T08:04:29+00:00",
"time": "2021-01-12T06:07:08+00:00",
"type": "library",
"installation-source": "dist",
"installation-source": "source",
"autoload": {
"psr-4": {
"phpspider\\": "./"
"IYUU\\Client\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
"GPL-3.0-or-later"
],
"authors": [
{
"name": "Seatle Yang",
"email": "seatle@foxmail.com",
"homepage": "http://www.phpspider.org",
"role": "Developer"
"name": "David",
"email": "367013672@qq.com"
}
],
"description": "The PHPSpider Framework.",
"homepage": "http://www.phpspider.org",
"description": "实现pt客户端管理的代码库",
"homepage": "https://github.com/ledccn/IYUUBittorrentClient",
"keywords": [
"framework",
"phpspider"
"qbittorrent",
"transmission"
]
}
]

View File

@ -27,7 +27,6 @@ tests-php5.6:
- php --version
- if [ ! -f composer.phar ]; then DOWLOAD_COMPOSER=1 ; fi;
- if [ -n "$DOWLOAD_COMPOSER" ] ; then php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" ; fi;
- if [ -n "$DOWLOAD_COMPOSER" ] ; then php -r "if (hash_file('sha384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" ; fi;
- if [ -n "$DOWLOAD_COMPOSER" ] ; then php composer-setup.php ; fi;
- if [ -n "$DOWLOAD_COMPOSER" ] ; then php -r "unlink('composer-setup.php');" ; fi;
- php composer.phar install
@ -36,7 +35,6 @@ tests-php5.6:
cache:
key: php5.6
paths:
- composer.phar
- vendor
tests-php7.0:
@ -51,7 +49,6 @@ tests-php7.0:
- php --version
- if [ ! -f composer.phar ]; then DOWLOAD_COMPOSER=1 ; fi;
- if [ -n "$DOWLOAD_COMPOSER" ] ; then php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" ; fi;
- if [ -n "$DOWLOAD_COMPOSER" ] ; then php -r "if (hash_file('sha384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" ; fi;
- if [ -n "$DOWLOAD_COMPOSER" ] ; then php composer-setup.php ; fi;
- if [ -n "$DOWLOAD_COMPOSER" ] ; then php -r "unlink('composer-setup.php');" ; fi;
- php composer.phar install
@ -75,7 +72,6 @@ tests-php7.1:
- php --version
- if [ ! -f composer.phar ]; then DOWLOAD_COMPOSER=1 ; fi;
- if [ -n "$DOWLOAD_COMPOSER" ] ; then php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" ; fi;
- if [ -n "$DOWLOAD_COMPOSER" ] ; then php -r "if (hash_file('sha384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" ; fi;
- if [ -n "$DOWLOAD_COMPOSER" ] ; then php composer-setup.php ; fi;
- if [ -n "$DOWLOAD_COMPOSER" ] ; then php -r "unlink('composer-setup.php');" ; fi;
- php composer.phar install
@ -89,25 +85,37 @@ tests-php7.1:
- vendor
tests-php7.2:
image: alpine:3.8
image: alpine:3.9
stage: test
services:
- name: "$CI_REGISTRY_IMAGE:server-test"
alias: server_test
script:
- apk add --no-cache php7-cli php7-curl php7-gd php7-phar php7-json php7-openssl php7-dom php7-simplexml php7-tokenizer php7-mbstring php7-xml
- apk add --no-cache composer php7-cli php7-curl php7-gd php7-phar php7-json php7-openssl php7-dom php7-simplexml php7-tokenizer php7-mbstring php7-xml
- php --version
- if [ ! -f composer.phar ]; then DOWLOAD_COMPOSER=1 ; fi;
- if [ -n "$DOWLOAD_COMPOSER" ] ; then php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" ; fi;
- if [ -n "$DOWLOAD_COMPOSER" ] ; then php -r "if (hash_file('sha384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" ; fi;
- if [ -n "$DOWLOAD_COMPOSER" ] ; then php composer-setup.php ; fi;
- if [ -n "$DOWLOAD_COMPOSER" ] ; then php -r "unlink('composer-setup.php');" ; fi;
- php composer.phar install
- composer install
- vendor/bin/phpcs --warning-severity=0 --standard=PSR2 src
- nohup php -S localhost:8000 -t tests/server/php-curl-test > phpd.log 2>&1 &
- vendor/bin/phpunit
cache:
key: php7.2
paths:
- composer.phar
- vendor
tests-php7.3:
image: alpine:3.11
stage: test
services:
- name: "$CI_REGISTRY_IMAGE:server-test"
alias: server_test
script:
- apk add --no-cache composer php7-cli php7-curl php7-gd php7-phar php7-json php7-openssl php7-dom php7-simplexml php7-tokenizer php7-mbstring php7-xml
- php --version
- composer install
- vendor/bin/phpcs --warning-severity=0 --standard=PSR2 src
- nohup php -S localhost:8000 -t tests/server/php-curl-test > phpd.log 2>&1 &
- vendor/bin/phpunit
cache:
key: php7.3
paths:
- vendor

View File

@ -78,12 +78,12 @@ class Curl
public $curl;
/**
* @var bool Whether an error occured or not
* @var bool Whether an error occurred or not
*/
public $error = false;
/**
* @var int Contains the error code of the curren request, 0 means no error happend
* @var int Contains the error code of the current request, 0 means no error happened
*/
public $error_code = 0;
@ -93,12 +93,12 @@ class Curl
public $error_message = null;
/**
* @var bool Whether an error occured or not
* @var bool Whether an error occurred or not
*/
public $curl_error = false;
/**
* @var int Contains the error code of the curren request, 0 means no error happend.
* @var int Contains the error code of the current request, 0 means no error happened.
* @see https://curl.haxx.se/libcurl/c/libcurl-errors.html
*/
public $curl_error_code = 0;
@ -109,7 +109,7 @@ class Curl
public $curl_error_message = null;
/**
* @var bool Whether an error occured or not
* @var bool Whether an error occurred or not
*/
public $http_error = false;
@ -124,17 +124,17 @@ class Curl
public $http_error_message = null;
/**
* @var string|array TBD (ensure type) Contains the request header informations
* @var string|array TBD (ensure type) Contains the request header information
*/
public $request_headers = null;
/**
* @var string|array TBD (ensure type) Contains the response header informations
* @var string|array TBD (ensure type) Contains the response header information
*/
public $response_headers = array();
/**
* @var string Contains the response from the curl request
* @var string|false|null Contains the response from the curl request
*/
public $response = null;
@ -146,12 +146,12 @@ class Curl
/**
* Constructor ensures the available curl extension is loaded.
*
* @throws \ErrorException
* @throws \RuntimeException
*/
public function __construct()
{
if (!extension_loaded('curl')) {
throw new \ErrorException('The cURL extensions is not loaded, make sure you have installed the cURL extension: https://php.net/manual/curl.setup.php');
throw new \RuntimeException('The cURL extensions is not loaded, make sure you have installed the cURL extension: https://php.net/manual/curl.setup.php');
}
$this->init();
@ -162,7 +162,7 @@ class Curl
/**
* Initializer for the curl resource.
*
* Is called by the __construct() of the class or when the curl request is reseted.
* Is called by the __construct() of the class or when the curl request is reset.
* @return self
*/
private function init()
@ -202,7 +202,7 @@ class Curl
// protected methods
/**
* Execute the curl request based on the respectiv settings.
* Execute the curl request based on the respective settings.
*
* @return int Returns the error code for the current curl request
*/
@ -212,14 +212,14 @@ class Curl
$this->response = curl_exec($this->curl);
$this->curl_error_code = curl_errno($this->curl);
$this->curl_error_message = curl_error($this->curl);
$this->curl_error = !($this->curl_error_code === 0);
$this->http_status_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
$this->http_error = in_array(floor($this->http_status_code / 100), array(4, 5));
$this->curl_error = !($this->getErrorCode() === 0);
$this->http_status_code = intval(curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$this->http_error = $this->isError();
$this->error = $this->curl_error || $this->http_error;
$this->error_code = $this->error ? ($this->curl_error ? $this->curl_error_code : $this->http_status_code) : 0;
$this->error_code = $this->error ? ($this->curl_error ? $this->getErrorCode() : $this->getHttpStatus()) : 0;
$this->request_headers = preg_split('/\r\n/', curl_getinfo($this->curl, CURLINFO_HEADER_OUT), null, PREG_SPLIT_NO_EMPTY);
$this->http_error_message = $this->error ? (isset($this->response_headers['0']) ? $this->response_headers['0'] : '') : '';
$this->error_message = $this->curl_error ? $this->curl_error_message : $this->http_error_message;
$this->error_message = $this->curl_error ? $this->getErrorMessage() : $this->http_error_message;
return $this->error_code;
}
@ -250,6 +250,18 @@ class Curl
$this->setOpt(CURLOPT_POSTFIELDS, $data);
}
/**
* Set the json payload informations to the postfield curl option.
*
* @param array $data The data to be sent.
* @return void
*/
protected function prepareJsonPayload(array $data)
{
$this->setOpt(CURLOPT_POST, true);
$this->setOpt(CURLOPT_POSTFIELDS, json_encode($data));
}
/**
* Set auth options for the current request.
*
@ -307,12 +319,17 @@ class Curl
*
* @param string $url The url to make the post request
* @param array $data Post data to pass to the url
* @param boolean $asJson Whether the data should be passed as json or not. {@insce 2.2.1}
* @return self
*/
public function post($url, $data = array())
public function post($url, $data = array(), $asJson = false)
{
$this->setOpt(CURLOPT_URL, $url);
$this->preparePayload($data);
if ($asJson) {
$this->prepareJsonPayload($data);
} else {
$this->preparePayload($data);
}
$this->exec();
return $this;
}
@ -320,7 +337,7 @@ class Curl
/**
* Make a put request with optional data.
*
* The put request data can be either sent via payload or as get paramters of the string.
* The put request data can be either sent via payload or as get parameters of the string.
*
* @param string $url The url to make the put request
* @param array $data Optional data to pass to the $url
@ -346,7 +363,7 @@ class Curl
/**
* Make a patch request with optional data.
*
* The patch request data can be either sent via payload or as get paramters of the string.
* The patch request data can be either sent via payload or as get parameters of the string.
*
* @param string $url The url to make the patch request
* @param array $data Optional data to pass to the $url
@ -398,7 +415,7 @@ class Curl
/**
* Pass basic auth data.
*
* If the the rquested url is secured by an httaccess basic auth mechanism you can use this method to provided the auth data.
* If the the requested url is secured by an htaccess basic auth mechanism you can use this method to provided the auth data.
*
* ```php
* $curl = new Curl();
@ -406,8 +423,8 @@ class Curl
* $curl->get('http://example.com/secure.php');
* ```
*
* @param string $username The username for the authentification
* @param string $password The password for the given username for the authentification
* @param string $username The username for the authentication
* @param string $password The password for the given username for the authentication
* @return self
*/
public function setBasicAuthentication($username, $password)
@ -418,7 +435,7 @@ class Curl
}
/**
* Provide optional header informations.
* Provide optional header information.
*
* In order to pass optional headers by key value pairing:
*
@ -442,7 +459,7 @@ class Curl
/**
* Provide a User Agent.
*
* In order to provide you cusomtized user agent name you can use this method.
* In order to provide you customized user agent name you can use this method.
*
* ```php
* $curl = new Curl();
@ -461,6 +478,9 @@ class Curl
/**
* @deprecated Call setReferer() instead
*
* @param $referrer
* @return self
*/
public function setReferrer($referrer)
{
@ -471,7 +491,7 @@ class Curl
/**
* Set the HTTP referer header.
*
* The $referer informations can help identify the requested client where the requested was made.
* The $referer Information can help identify the requested client where the requested was made.
*
* @param string $referer An url to pass and will be set as referer header
* @return self
@ -503,24 +523,26 @@ class Curl
*
* @see http://php.net/curl_setopt
*
* @param int $option The curl option constante e.g. `CURLOPT_AUTOREFERER`, `CURLOPT_COOKIESESSION`
* @param mixed $value The value to pass for the given $option
* @param int $option The curl option constant e.g. `CURLOPT_AUTOREFERER`, `CURLOPT_COOKIESESSION`
* @param mixed $value The value to pass for the given $option
* @return bool
*/
public function setOpt($option, $value)
{
return curl_setopt($this->curl, $option, $value);
}
/**
* Get customized curl options.
*
* To see a full list of options: http://php.net/curl_getinfo
*
* @see http://php.net/curl_getinfo
*
* @param int $option The curl option constante e.g. `CURLOPT_AUTOREFERER`, `CURLOPT_COOKIESESSION`
* @param mixed $value The value to check for the given $option
*/
* Get customized curl options.
*
* To see a full list of options: http://php.net/curl_getinfo
*
* @see http://php.net/curl_getinfo
*
* @param int $option The curl option constant e.g. `CURLOPT_AUTOREFERER`, `CURLOPT_COOKIESESSION`
* @param mixed The value to check for the given $option
* @return mixed
*/
public function getOpt($option)
{
return curl_getinfo($this->curl, $option);
@ -539,17 +561,26 @@ class Curl
}
/**
* Enable verbositiy.
* Enable verbosity.
*
* @todo As to keep naming convention it should be renamed to `setVerbose()`
* @param bool $on
* @return self
*/
public function setVerbose($on = true)
{
$this->setOpt(CURLOPT_VERBOSE, $on);
return $this;
}
/**
* @deprecated Call setVerbose() instead
*
* @param string $on
* @param bool $on
* @return self
*/
public function verbose($on = true)
{
$this->setOpt(CURLOPT_VERBOSE, $on);
return $this;
return $this->setVerbose($on);
}
/**
@ -574,7 +605,7 @@ class Curl
$this->http_error_message = null;
$this->request_headers = null;
$this->response_headers = array();
$this->response = null;
$this->response = false;
$this->init();
return $this;
}
@ -605,7 +636,7 @@ class Curl
*/
public function isInfo()
{
return $this->http_status_code >= 100 && $this->http_status_code < 200;
return $this->getHttpStatus() >= 100 && $this->getHttpStatus() < 200;
}
/**
@ -614,7 +645,7 @@ class Curl
*/
public function isSuccess()
{
return $this->http_status_code >= 200 && $this->http_status_code < 300;
return $this->getHttpStatus() >= 200 && $this->getHttpStatus() < 300;
}
/**
@ -623,7 +654,7 @@ class Curl
*/
public function isRedirect()
{
return $this->http_status_code >= 300 && $this->http_status_code < 400;
return $this->getHttpStatus() >= 300 && $this->getHttpStatus() < 400;
}
/**
@ -632,7 +663,7 @@ class Curl
*/
public function isError()
{
return $this->http_status_code >= 400 && $this->http_status_code < 600;
return $this->getHttpStatus() >= 400 && $this->getHttpStatus() < 600;
}
/**
@ -641,7 +672,7 @@ class Curl
*/
public function isClientError()
{
return $this->http_status_code >= 400 && $this->http_status_code < 500;
return $this->getHttpStatus() >= 400 && $this->getHttpStatus() < 500;
}
/**
@ -650,7 +681,7 @@ class Curl
*/
public function isServerError()
{
return $this->http_status_code >= 500 && $this->http_status_code < 600;
return $this->getHttpStatus() >= 500 && $this->getHttpStatus() < 600;
}
/**
@ -673,7 +704,7 @@ class Curl
* ```
*
* @param string $headerKey Optional key to get from the array.
* @return bool|string
* @return bool|string|array
* @since 1.9
*/
public function getResponseHeaders($headerKey = null)
@ -697,21 +728,37 @@ class Curl
return $headers;
}
/**
* Get response from the curl request
* @return string|false
*/
public function getResponse()
{
return $this->response;
}
/**
* Get curl error code
* @return string
*/
public function getErrorCode()
{
return $this->curl_error_code;
}
/**
* Get curl error message
* @return string
*/
public function getErrorMessage()
{
return $this->curl_error_message;
}
/**
* Get http status code from the curl request
* @return int
*/
public function getHttpStatus()
{
return $this->http_status_code;