多くの人々は、phpはサイトの開発にのみ適しており、プログラミング言語のアプリケーションの他の領域、プログラムの作成には使用できないと考えています...画面を作成し、yandexディスクにアップロードし、コンソールにスクリーンショットアドレスを表示するスクリプト...
プロジェクトの構造を考えてみましょう。非常に単純で、3つのファイルで構成されています。
1. screen.php-アプリケーションへのエントリポイント。
2. classes / autoload.php-プロジェクトのオートローダー。
3. classes / Request.php-Yandex APIへのリクエストを実装するクラス。
次に、screen.phpコードを検討します。
これがアプリケーションへのエントリポイントであることがわかるように、ロジックは単純です。
1.スクリーンショット名の形成
2. scrotシステムプログラムの呼び出し
3. api yandex.diskをリクエストし、スクリーンショットをアップロードします
autoload.phpファイルも非常にシンプルで、3行のコードのみで構成されています。レビューのためだけに提供します。詳細については検討しません。
spl_autoload_register(function($name){ require_once __DIR__.'/'.$name.'.php'; });
yandex apiの操作は非常に簡単です。小さなクラスのRequest.phpを作成し、それを操作するのに役立つ特定のメソッドのセットを...
Request.phpのリスト <?php class Request { private $_token = null; private $_href = null; private $_method = null; private $_filePath = null; private $_fileName = null; public function getOauthLink() { $link = 'https://oauth.yandex.ru/authorize' .'?response_type=token' . '&client_id=8fc231e60575439fafcdb3b9281778a3'; echo $link; } public function setFileNameOnDisk($name) { $link = 'https://cloud-api.yandex.net/v1/disk/resources/upload?path='.urlencode('/'.trim($name,'/')); $response = file_get_contents($link,false,$this->_context('GET')); $responseAsArray = json_decode($response,true); $this->_href = $responseAsArray['href']; $this->_method = $responseAsArray['method']; $this->_fileName = $name; return $this; } public function setPathToFile($path) { $this->_filePath = $path; return $this; } public function upload() { $ch = curl_init($this->_href); curl_setopt($ch,CURLOPT_HTTPHEADER, array( 'Authorization', 'OAuth '.$this->_token ) ); curl_setopt($ch,CURLOPT_INFILE,fopen($this->_filePath,"r")); curl_setopt($ch,CURLOPT_INFILESIZE,filesize($this->_filePath)); curl_setopt($ch,CURLOPT_PUT,true); curl_exec($ch); curl_close($ch); return $this; } public function publicateFile() { $link = 'https://cloud-api.yandex.net/v1/disk/resources/publish?path='.urlencode('/'.trim($this->_fileName,'/')); $response = file_get_contents($link,false,$this->_context('PUT')); $responseAsArray = json_decode($response,true); $publicateFile = file_get_contents($responseAsArray['href'],false,$this->_context($responseAsArray['method'])); $publicateFileAsArray = json_decode($publicateFile,true); return $publicateFileAsArray; } public function setToken($token) { $this->_token = $token; return $this; } private function _context($method) { $opts = array( 'http'=>array( 'method'=>$method, 'header'=>"Authorization: OAuth ".$this->_token."\r\n" ) ); $context = stream_context_create($opts); return $context; } }
私は主にfile_get_contentsを使用し、多くのメソッドを要求するときに使用するため、コンテキスト生成メソッドを作成しました。
private function _context($method) { $opts = array( 'http'=>array( 'method'=>$method, 'header'=>"Authorization: OAuth ".$this->_token."\r\n" ) ); $context = stream_context_create($opts); return $context; }
また、特定のリクエストメソッドと認証情報を使用してコンテキストを作成します...
次に、「yandex.diskにファイルを作成する」必要があります。次の方法でこのアクションを実行します。
public function setFileNameOnDisk($name) { $link = 'https://cloud-api.yandex.net/v1/disk/resources/upload?path='.urlencode('/'.trim($name,'/')); $response = file_get_contents($link,false,$this->_context('GET')); $responseAsArray = json_decode($response,true); $this->_href = $responseAsArray['href']; $this->_method = $responseAsArray['method']; $this->_fileName = $name; return $this; }
前に言ったように、file_get_contents関数を使用してAPIをリクエストします。 このメソッドが機能し、すべての情報が要求されると、アップロードメソッドが開始されます。
public function upload() { $ch = curl_init($this->_href); curl_setopt($ch,CURLOPT_HTTPHEADER, array( 'Authorization', 'OAuth '.$this->_token ) ); curl_setopt($ch,CURLOPT_INFILE,fopen($this->_filePath,"r")); curl_setopt($ch,CURLOPT_INFILESIZE,filesize($this->_filePath)); curl_setopt($ch,CURLOPT_PUT,true); curl_exec($ch); curl_close($ch); return $this; }
この場合、関数 `file_get_contents`または` file_put_contents`のいずれかを使用してファイルを送信できますが、これらの関数のコンテキストではヘッダーやその他の問題を手動でシミュレートする必要があるため、これはお勧めできません。これにはcurlを使用する方が簡単です。
そして、ファイルはアップロードされます。残っているのは、それを公開し、それを表示するための直接リンクを取得することだけです。関数publicateFile()を実行します。
public function publicateFile() { $link = 'https://cloud-api.yandex.net/v1/disk/resources/publish?path='.urlencode('/'.trim($this->_fileName,'/')); $response = file_get_contents($link,false,$this->_context('PUT')); $responseAsArray = json_decode($response,true); $publicateFile = file_get_contents($responseAsArray['href'],false,$this->_context($responseAsArray['method'])); $publicateFileAsArray = json_decode($publicateFile,true); return $publicateFileAsArray; }
このメソッドでは、すべてが非常に簡単です。APIからPUTメソッドを使用してファイルの公開をリクエストし、Yandexは公開を確認するリクエストを実行する必要があるリンクを返し、リクエストメソッドをディスクに返します。 最後に、2番目の要求の後、パブリックファイルへのリンクを含む配列を取得します。
スクリプトのインストール
さて、これで完了です。このスクリプトがコンソールからどのように機能するかを理解する必要がありますか 「グローバルエリア」でそれを起動する方法は? これらの質問に対する答えは、phar-phpファイルを含むアーカイブで、同じjarに似た別のアプリケーションとして実行できます。
box.pharユーティリティを使用してpharを構築します;このために、簡単なbox.json設定ファイルを作成します。
{ "files": [ "classis/autoload.php", "classis/Request.php", "screen.php" ], "main": "screen.php", "output": "srcphp.phar", "stub": true }
ビルドするには、次を実行します:
$ php box.phar build
これで、プロジェクトはファイルを実行する権限を設定するだけで、/ usr / bin / srcphpディレクトリにコピーする準備ができました。
$ chmod +x srcphp.phar $ cp srcphp.phar /usr/bin/srcphp
/home/myname/.config/srcphp/config.phpファイルの構成を忘れないでください:
<?php
トークンでは、キー--getTokenを使用してスクリプト起動ツールによって生成された切り替え時に、Yandexから受信したoAuthトークンを入力する必要があります。
$ srcphp --getToken
結論
この記事の主なアイデアは、スクリーンショットを例に使用して、phpを使用してコンソールアプリケーションを作成する方法を検討することです。以下では、アプリケーションのさまざまな分野でphpを使用するトピックを取り上げます。 ご清聴ありがとうございました。
ps アプリケーションのソースコード