PowerShellにcurlがあることを知らなかった
powershellにもcurlがあることを知り試してみた。
curlはInvoke-WebRequestのAliasで以下のページを参考にした。
といってもURIオプションしか参考にしていないが・・・
docs.microsoft.com
以下のようなJSONデータを返す自前で用意したPHPのページへリクエストを出してみた。
[ { "id":1, "code":"001", "title":"一流の言いかえ−「ふつうの人」を「品のいい人」に変える−", "publisher":"光文社", "year":"2022" }, { "id":2, "code":"001", "title":"老いの品格−品よく、賢く、おもしろく−(PHP新書 1310)", "publisher":"PHP研究所", "year":"2022" }, { "id":3, "code":"001", "title":"「させていただく」の使い方−日本語と敬語のゆくえ−(角川新書 K−381)", "publisher":"KADOKAWA", "year":"2022" } ]
powershellのcurlでリクエストしたときのtranscript
PS >get-command curl CommandType Name Version Source ----------- ---- ------- ------ Alias curl -> Invoke-WebRequest PS >$r = curl -URI http://localhost/php/sqlite3json_newbook.php PS >$r.gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True False HtmlWebResponseObject Microsoft.PowerShell.Commands.WebResponseObject PS >$r | get-member TypeName: Microsoft.PowerShell.Commands.HtmlWebResponseObject Name MemberType Definition ---- ---------- ---------- Dispose Method void Dispose(), void IDisposable.Dispose() Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() AllElements Property Microsoft.PowerShell.Commands.WebCmdletElementCollection AllElements {get;} BaseResponse Property System.Net.WebResponse BaseResponse {get;set;} Content Property string Content {get;} Forms Property Microsoft.PowerShell.Commands.FormObjectCollection Forms {get;} Headers Property System.Collections.Generic.Dictionary[string,string] Headers {get;} Images Property Microsoft.PowerShell.Commands.WebCmdletElementCollection Images {get;} InputFields Property Microsoft.PowerShell.Commands.WebCmdletElementCollection InputFields {get;} Links Property Microsoft.PowerShell.Commands.WebCmdletElementCollection Links {get;} ParsedHtml Property mshtml.IHTMLDocument2 ParsedHtml {get;} RawContent Property string RawContent {get;set;} RawContentLength Property long RawContentLength {get;} RawContentStream Property System.IO.MemoryStream RawContentStream {get;} Scripts Property Microsoft.PowerShell.Commands.WebCmdletElementCollection Scripts {get;} StatusCode Property int StatusCode {get;} StatusDescription Property string StatusDescription {get;} PS >$r StatusCode : 200 StatusDescription : OK Content : [{"id":1,"code":"001","title":"一流の言いかえ−「ふつうの人」を「品のいい人」に変える−","publisher":"光文社","year":"2022"},{"id":2,"code":"001","title" :"老いの品格−品よく、賢く、おもしろく−(PHP新書 1310)","publisher":"PHP研究所","year":"2022"},{"id":3... RawContent : HTTP/1.1 200 OK Content-Length: 522 Content-Type: application/json; charset=utf-8 Date: Mon, 12 Sep 2022 06:18:51 GMT Server: Microsoft-IIS/10.0 X-Powered-By: PHP/8.0.11 [{"id":1,"code":"001",... Forms : {} Headers : {[Content-Length, 522], [Content-Type, application/json; charset=utf-8], [Date, Mon, 12 Sep 2022 06:18:51 GMT], [Server, Microsoft-IIS/10.0]...} Images : {} InputFields : {} Links : {} ParsedHtml : mshtml.HTMLDocumentClass RawContentLength : 522 PS >$c = ( $r.content | convertfrom-json ) PS >$c id : 1 code : 001 title : 一流の言いかえ−「ふつうの人」を「品のいい人」に変える− publisher : 光文社 year : 2022 id : 2 code : 001 title : 老いの品格−品よく、賢く、おもしろく−(PHP新書 1310) publisher : PHP研究所 year : 2022 id : 3 code : 001 title : 「させていただく」の使い方−日本語と敬語のゆくえ−(角川新書 K−381) publisher : KADOKAWA year : 2022 PS >$psversiontable Name Value ---- ----- PSVersion 5.1.19041.1682 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.19041.1682 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 PS >stop-transcript
PHPのページのコード
<?php header("Content-Type: application/json; charset=utf-8"); $file = 'db/newbook.sqlite'; $db = new SQLite3($file); $sql = "SELECT * FROM book order by id limit 3"; $result = $db->query($sql); while($res = $result->fetchArray(SQLITE3_ASSOC)){ $h[] = array('id'=>$res['id'],'code'=>$res['code'],'title'=>$res['title'],'publisher'=>$res['publisher'],'year'=>$res['year']); } $db = null; $js =json_encode($h, JSON_UNESCAPED_UNICODE); print_r($js);
了