OSDN Git Service

7e9465b974e9b7fbf1e46bfe5fbb47ba6ac30a3d
[ring-lang-081/ring.git] / docs / build / html / _sources / libcurl.txt
1 .. index:: 
2         single: RingLibCurl; はじめに
3
4 ==================
5 RingLibCurl の用法
6 ==================
7
8 RingLibCurl の用法を学びます。
9
10 .. index:: 
11         pair: RingLibCurl; Get リクエスト
12
13 Get リクエスト
14 ==============
15
16 用例:
17
18 .. code-block:: ring
19
20         load "libcurl.ring"     
21
22         curl = curl_easy_init()
23
24         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1)
25         curl_easy_setopt(curl, CURLOPT_URL, "http://ring-lang.sf.net")
26
27         curl_easy_perform(curl)
28
29         curl_easy_cleanup(curl)
30
31
32 .. index:: 
33         pair: RingLibCurl; Post リクエスト
34
35 Post リクエスト
36 ===============
37
38 用例:
39
40 .. code-block:: ring
41
42         load "libcurl.ring"
43
44         curl = curl_easy_init()
45
46         cPostThis = "page=4&Number1=4&Number2=5"
47         curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/ringapp/index.ring?page=3")
48         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, cPostThis)
49
50         curl_easy_perform(curl)
51
52         curl_easy_cleanup(curl)
53
54 .. index:: 
55         pair: RingLibCurl; Facebook へのログイン
56
57 Facebook へのログイン
58 =====================
59
60 用例:
61
62 .. code-block:: ring
63
64         load "libcurl.ring"
65
66         see "Enter Email : " give $login_email 
67         See "Enter Password : " give $login_pass
68
69         curl = curl_easy_init()
70
71         curl_easy_setopt(curl, CURLOPT_URL, 'https://www.facebook.com/login.php')
72         curl_easy_setopt(curl, CURLOPT_POSTFIELDS,'charset_test=j u s t a t e s t'+
73         ' &email='+urlencode($login_email)+'&pass='+
74         urlencode($login_pass)+'&login=Login')
75         curl_easy_setopt(curl, CURLOPT_POST, 1)
76         curl_easy_setopt(curl, CURLOPT_HEADER, 0)
77         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1)
78         curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt")
79         curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookies.txt")
80         curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U;"+
81         " Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3")
82         curl_easy_setopt(curl, CURLOPT_REFERER, "http://www.facebook.com")
83         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE)
84         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2)
85
86         mylist = curl_slist_append(NULL,'Accept-Charset: utf-8')
87         curl_slist_append(mylist,'Accept-Language: en-us,en;q=0.7,bn-bd;q=0.3')
88         curl_slist_append(mylist,'Accept: text/xml,application/xml,'+
89         'application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5')
90         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, mylist)
91
92         curl_easy_setopt(curl, CURLOPT_COOKIESESSION, false)
93
94         curl_easy_perform(curl)
95
96         curl_easy_cleanup(curl)
97
98         Func URLEncode cStr
99                 cOut = ""
100                 for x in cStr
101                         if isalnum(x)
102                                 cOut += x
103                         but x = " "
104                                 cOut += "+"
105                         else
106                                 cOut += "%"+str2hex(x)
107                         ok
108                 next
109                 return cOut     
110
111 .. index:: 
112         pair: RingLibCurl; 実行結果を文字列で保存
113
114 実行結果を文字列で保存
115 ======================
116
117 用例:
118
119 .. code-block:: ring
120
121         load "libcurl.ring"
122
123         curl = curl_easy_init()
124
125         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1)
126         curl_easy_setopt(curl, CURLOPT_URL, "http://ring-lang.sf.net")
127
128         cOutput = curl_easy_perform_silent(curl)
129
130         See "Output:" + nl
131         see cOutput
132
133         curl_easy_cleanup(curl)
134
135 .. index:: 
136         pair: RingLibCurl; Yahoo! から株式情報を取得
137
138 Yahoo! から株式情報を取得
139 =========================
140
141 用例:
142
143 .. code-block:: ring
144
145         Load "libcurl.ring"
146
147         ### 第一部 --- 断片とクッキーの取得 -----------------------------------------
148
149         See "Start curl_easy_init(): "+ nl
150         curl = curl_easy_init()                     ### >>> HANDLE >>> 01006BD0  CURL  0
151
152                 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1)
153                 curl_easy_setopt(curl, CURLOPT_COOKIEJAR,  "cookies.txt")
154                 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookies.txt")
155                 curl_easy_setopt(curl, CURLOPT_URL, "https://finance.yahoo.com/quote/AMZN/history")
156
157                 ###  HTML データ >>> STDOUT Window では curl_easy_perform_silent >>> String を使用。
158
159         cOutput = curl_easy_perform_silent(curl)    ### データの取得 >>> String
160
161
162         ###   データから断片 (Crumb) を抽出します。
163         ###  "CrumbStore":{"crumb":"abcdefghijk"},
164
165         if cOutput != NULL
166
167                 newStr1     = substr(cOutput, substr(cOutput, '"CrumbStore":{"crumb":"' ), 48 )
168                         nPosS   = substr(newStr1, ':"' ) ;  ### crumb -2 の始点
169                         nPosE   = substr(newStr1, '"}' ) ;  ### crumb の終点
170                         nCount  = nPosE - nPosS -2          ### crumb の大きさ
171                 myCrumb     = substr(newStr1, nPosS +2, nCount)
172
173                 See "myCrumb.: |"+ myCrumb +"|" +nl
174                 
175                 ### UniCode の "\u002F" を "/" へ置換します。
176                         if substr( myCrumb, "\u002F")
177                            myCrumb = substr( myCrumb, "\u002F", "/")
178                            See "myCrumb2: |"+ myCrumb +"|"+ nl
179                         ok
180
181         else
182                 See "No Connectivity to Yahoo. Looking for Cookie and Crumb." +nl +nl
183         ok
184
185                                         
186         ### 第二部 --- URL へ断片とクッキーを送信 ----------------------------------------------
187
188                 ### Yahoo! へ URL+断片を送信して最初の株式履歴情報を取得します。
189
190                 $url = "https://query1.finance.yahoo.com/v7/finance/download/AMZN"+
191                         "?period1=1277856000&period2=1498777545&interval=1wk" + 
192                         "&events=history&crumb=" + myCrumb
193
194                 curl_easy_setopt(curl, CURLOPT_URL, $url);
195                 cStr = curl_easy_perform_silent(curl)
196                 See cStr 
197
198         curl_easy_cleanup(curl)  ### cURL を閉じるのを忘れないように。
199
200
201 実行結果:
202
203 .. code-block:: ring
204
205         myCrumb.: |sEEeW97mxvN|
206         Date,Open,High,Low,Close,Adj Close,Volume
207         2010-07-05,110.650002,117.480003,109.000000,117.260002,117.260002,21000400
208         2010-07-12,117.809998,124.879997,117.320000,118.489998,118.489998,29407300
209         2010-07-19,118.379997,121.250000,105.800003,118.870003,118.870003,74252100