PHP CURL 登入 SR-Webs ?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
header("Content-type: text/html;charset=utf-8");

$loginData = array(
'username' => '',
'password' => ''
);

$structData= array(
'url' => 'http://sr-webs.com/bbs.cgi',
'postFields' => 'userid='.urlencode($loginData['username']).'&userpassword='.urlencode($loginData['password']),
'header' => 0,
'followLocation' => 1,
'cookiejar' => './tmp/coo.txt'
);

//
$ch = curl_init();
$options = array(
CURLOPT_URL => $structData['url'],
CURLOPT_POSTFIELDS => $structData['postFields'],
CURLOPT_POST => 1,
CURLOPT_HEADER => $structData['header'],
CURLOPT_FOLLOWLOCATION => $structData['followLocation'],
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_COOKIEJAR => $structData['cookiejar'],
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"
);
curl_setopt_array($ch, $options);
$d = curl_exec($ch);
if(curl_errno($ch) == 0) file_put_contents("tmp/data.txt", $d);
curl_close($ch);

// file_get_contents($structData['cookiejar']);

// 來源註解 : http://bbs.phpchina.com/viewthread.php?tid=70219
//
//CURLOPT_URL 是要得到內容的地址
//CURLOPT_HEADER 是要得到的頭
//CURLOPT_NOBODY 是要得到的內容
//CURLOPT_PORT 是端口
//CURLOPT_RETURNTRANSFER 是要把輸出的內容放到buffer中,可以被echo或者賦予某變量
//CURLOPT_POST 是標準的提交
//CURLOPT_POSTFIELDS 是提交的內容
//CURLOPT_COOKIEJAR 是要保存的cookie
//CURLOPT_COOKIEFILE 是從文件讀取cookie並提交
//CURLOPT_FOLLOWLOCATION 啟用時會將服務器服務器返回的「Location:」放在header中遞歸的返回給服務器
//CURLOPT_MAXREDIRS 限定遞歸返回的數量
//CURLOPT_HTTPHEADER 設置一個header中傳輸內容的數組
//CURLOPT_REFERER 設置header中"Referer:"部分的值
//CURLOPT_USERAGENT 在HTTP請求中包含一個」user-agent」頭的字符串。
//CURLOPT_ENCODING 設定header中「Accept-Encoding: 」部分的內容,支持的編碼格式為:"identity","deflate","gzip"。如果設置為空字符串,則表示支持所有的編碼格式
//CURLOPT_COOKIE 設定HTTP請求中Set-Cookie:部分的內容
?>

參考資料 :
http://blog.roodo.com/rocksaying/archives/2102451.html
http://coding.derkeiler.com/Archive/Perl/comp.lang.perl.misc/2008-05/msg01095.html (Perl ?)
http://codesnippets.joyent.com/posts/show/1204 (PHP ?)
http://hi.baidu.com/camark/blog/item/82ee2ca82071beb7ca130c27.html (Python ?)