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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
//HTTPClient.php
<?php
class HTTPClient
{
public $host = "www.polidog.jp";
public $port = 80;
private $postData = array();
public function sendGet($path=”,$body=”){
$ret = null;
try{
//値チェック
if(empty($path)){throw new Exception("pathが存在しません");}
if(empty($this->host)){throw new Exception("ホスト名が存在しません");}
if(!is_numeric($this->port)){throw new Exception("ポート番号が不正です");}
$fp = fsockopen( $this->host, $this->port, $errno, $errstr, 30);
if(!$fp){throw new Exception("HTTP接続ができません");}
$http_header .= "GET $path HTTP/1.0\r\n";
$http_header .= "Host: ".$this->host."\r\n";
$http_header .= "Accept: */*\r\n";
$http_header .= "Connection: Close\r\n\r\n";
fwrite($fp,$http_header);
while ( !feof( $fp ) ) {
$ret .= fgets( $fp, 128 );
}
fclose( $fp );
}catch(Exception $e){
print $e->getMessage();
exit();
}
return $ret;
}
public function sendPost($path=null,$post= null){
try{
if(empty($path)){throw new Exception("pathが存在しません");}
if(empty($this->host)){throw new Exception("ホスト名が存在しません");}
if(!is_numeric($this->port)){throw new Exception("ポート番号が不正です");}
if (is_null($post)) {
$post = $this->postData;
}
foreach($post as $key => $value){
$send_data_array[] = urlencode($key)."=".urlencode($value);
}
$send_data = implode("&",$send_data_array);
$length = strlen($send_data);
$http_header .= "POST $path HTTP/1.0\r\n";
$http_header .= "Host: ".$this->host."\r\n";
$http_header .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
$http_header .= "Accept-Language: ja,en-us;q=0.7,en;q=0.3\r\n";
$http_header .= "Accept-Encoding: gzip,deflate\r\n";
$http_header .= "Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7\r\n";
$http_header .= "Keep-Alive: 300\r\n";
$http_header .= "Connection: keep-alive\r\n";
$http_header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$http_header .= "Content-Length: $length\r\n\r\n";
$http_header .= $send_data;
$fp = fsockopen( $this->host, $this->port, $errno, $errstr, 30);
fwrite($fp,$http_header);
while ( !feof( $fp ) ) {
$ret .= fgets( $fp, 128 );
}
fclose( $fp );
return $ret;
}catch(Exception $e){
print $e->getMessage();
exit();
}
}
function addPostData($key = null, $value = null){
if(is_null($key) || is_null($value)){
return false;
}
$this->postData[$key] = $value;
return true;
}
}
?>
|