curl紹介とよくある使い方

3806 ワード

本文では、curlに関する知識と一般的な使い方を紹介します。
1.概要
1.1 what
curl is a command Library for transferring data with URLs.
1.2 What's curl used for
curl is used in command lines or scripts to transfer data. 
curl is also used in cars,television sets,routers,printers,audio equipment,mobile phons,tables,settop boss,media plears and is the internet trance backbone for thours sands of Software aftware aphons。
1.3 curl supports
curl supports DICT,FILE,FTP,FTPS,Gopher,HTTP,HTTPS,IMAP,IMAPS,LDAP,LDAPS,POP 3,POP 3 S,RTMP,RTSP,SCP,SFTP,SMB,SMBS,SMTP,SMTPS,Telnet TFP. 
curl supports SSL certificates、HTTP POST、HTTP PUT、FTP uloading、HTTP form based uplload、proxies、HTTP/2、cookies、user+password authentication(Baic、Plain、Digest、CRAM-Mand 5、NTLM、Nerfistration)
2.よくある使い方
2.1ページを取得する
使い方:curl URL
例は以下の通りです
curl https://www.baidu.com/
これはcurlの一番簡単な使い方です。上のコマンドを実行して取得できます。https://www.baidu.com/ 同じページを指しています。ここのURLが一つのファイルまたは一枚の図を指しているなら、直接にローカルにダウンロードできます。
2.2 POST要求を送信する
使い方:curl-X POST-d'post content' URL
-Xオプションの説明は以下の通りです。
-X/--request 
      (HTTP) Specifies a custom request method to use when communicating with the HTTP 
server.  The specified request will be used instead of the method otherwise used (which 
defaults to GET). Read the HTTP 1.1 specification for details and explanations.
      (FTP) Specifies a custom FTP command to use instead of LIST when doing file lists 
with ftp.
      If this option is used several times, the last one will be used.
-dオプションは以下の通りです。
-d/--data 
      (HTTP) Sends the specified data in a POST request to the HTTP server, in a way that 
can emulate as if a user has filled in a HTML form and pressed the submit button. Note that 
the data is sent exactly as specified with no extra processing (with all newlines cut off). 
The data is expected to be "url-encoded". This will cause curl to pass the data to the 
server using the content-type application/x-www-form-urlencoded. Compare to -F/--form. If 
this option is used more than once on the same command line, the data pieces specified will 
be merged together with a separating &-letter. Thus, using '-d name=daniel -d skill=lousy' 
would generate a post chunk that looks like 'name=daniel&skill=lousy'.
例は以下の通りです
curl -X -d 'api_ver=1.0.0&merchant_id=1800257711' http://api.gcdev.tfb8.com/cgi-bin/v2.0/api_merchant_agency_qry.cgi
注意:-dオプションの後の内容は、二重引用符ではなく単引用符で含めることを強く推奨します。
2.3 GET要求の送信
使い方:curl URL?param 1=value 1\¶m 2=value 2…
例は以下の通りです
curl http://api.gcdev.tfb8.com/cgi-bin/v2.0/api_merchant_agency_qry.cgi?api_ver=1.0.0\&merchant_id=1800257711
注意:shellでcurlコマンドを実行して、GET要求を送信する場合、複数のパラメータを送信するには、パラメータ間のセパレータ「&」を「\&」に変更する必要があります。そうでないと、shellはセパレータ「&」をバックグラウンド実行コマンドとして理解し、GET要求パラメータを遮断します。
2.4リンクリダイレクト/自動ジャンプ(-L)
curlを使用して、リダイレクトされたリンクを開くと、私たちが欲しいウェブページの内容が得られないかもしれません。リンクがあると仮定しますhttp://www.A.comを選択します。ブラウザを使ってリンクにアクセスすると、自動的にリンクにジャンプします。http://www.B.com。もしcurlを使って直接Aリンクにアクセスすれば、以下のようになります。
curl http://www.A.com
Aリンクは存在しません。AリンクはBリンクにリダイレクトされましたが、curlは認識できませんでした。
この場合は-Lオプションを使う必要があります。「curl-L」を使って、curlの開いたリンクを自動的にジャンプさせ、最終ページにアクセスすることができます。以下のとおりです
curl -L http://www.A.com
上のコマンドを使ってリンクAにアクセスすると、自動的にそのリダイレクトされたリンクBにジャンプして、最終的にリンクBの内容を取得します。
2.5 ページ応答をファイルに書き込む(-o)
curlを使用してウェブページの応答メッセージをファイルに書き込むと、以下のように「−o」オプションがあります。
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
上のコマンドはリンクされます。 http://mirrors.aliyun.com/repo/Centos-7.repo の内容応答情報を /etc/yum.repos.d/CentOS-Base.repoファイルにあります。