@@ -17,6 +17,7 @@ class Http
1717 * @var array 公用的URL参数
1818 */
1919 public $ query = array ();
20+ public $ http_prefix = 'http:// ' ;
2021
2122 /**
2223 * http的语法
@@ -28,34 +29,103 @@ class Http
2829 *
2930 * @return string
3031 */
31- public function request ($ url , $ param = '' , $ method = 'GET ' , $ header = array ())
32+ public function request ($ url , array $ param = array () , $ method = 'GET ' , $ header = array ())
3233 {
33- $ url = 'http:// ' . $ url ;
34- $ get = $ this ->query ;
35- if (strcasecmp ($ method , 'get ' ) === 0 && is_array ($ param )) {
36- $ get = array_merge ($ get , $ param );
37- $ param = '' ;
34+ $ query = array ();
35+ $ header = array_merge ($ header , $ this ->headers );
36+ $ body = '' ;
37+ if (!empty ($ param ['__body ' ])) {
38+ $ body = $ param ['__body ' ];
39+ unset($ param ['__body ' ]);
40+ }
41+ if (strcasecmp ($ method , 'get ' ) === 0 ) {
42+ $ query = array_merge ($ this ->query , $ param );
43+ } else {
44+ if (empty ($ body )) {
45+ $ body = http_build_query ($ param );
46+ $ query = array_merge ($ this ->query );
47+ } else {
48+ $ query = array_merge ($ this ->query , $ param );
49+ }
50+ }
51+ if (strpos ($ url , '? ' ) === false ) {
52+ $ url .= '? ' . http_build_query ($ query );
53+ } else {
54+ $ url .= '& ' . http_build_query ($ query );
55+ }
56+
57+ $ ch = curl_init ();
58+ if ($ this ->http_prefix == 'https:// ' ) {
59+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , false );
60+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , false );
61+ }
62+ curl_setopt ($ ch , CURLOPT_URL , $ this ->http_prefix . $ url );
63+ curl_setopt ($ ch , CURLOPT_CUSTOMREQUEST , $ method ); //设置请求方式
64+ curl_setopt ($ ch , CURLOPT_HEADER , false );
65+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
66+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ header );
67+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ body );
68+ $ content = curl_exec ($ ch );
69+ $ status = curl_errno ($ ch );
70+ if ($ status === 0 ) {
71+ return $ content ;
72+ } else {
73+ return false ;
74+ }
75+ }
76+
77+ public function header ($ url , array $ param = array (), $ method = 'GET ' , $ header = array ())
78+ {
79+ $ query = array ();
80+ $ header = array_merge ($ header , $ this ->headers );
81+ $ body = '' ;
82+ if (!empty ($ param ['__body ' ])) {
83+ $ body = $ param ['__body ' ];
84+ unset($ param ['__body ' ]);
85+ }
86+ if (strcasecmp ($ method , 'get ' ) === 0 ) {
87+ $ query = array_merge ($ this ->query , $ param );
88+ } else {
89+ if (empty ($ body )) {
90+ $ body = http_build_query ($ param );
91+ $ query = array_merge ($ this ->query );
92+ } else {
93+ $ query = array_merge ($ this ->query , $ param );
94+ }
3895 }
3996 if (strpos ($ url , '? ' ) === false ) {
40- $ url .= '? ' . http_build_query ($ get );
97+ $ url .= '? ' . http_build_query ($ query );
4198 } else {
42- $ url .= '& ' . http_build_query ($ get );
43- }
44- if (is_array ($ param )) {
45- $ param = http_build_query ($ param );
46- }
47- $ http ['method ' ] = $ method ;
48- $ http ['header ' ] = '' ;
49- $ header ['Content-Length ' ] = strlen ($ param );
50- $ header = array_replace ($ this ->headers , $ header );
51- foreach ($ header as $ k => $ v ) {
52- if (!empty ($ v )) {
53- $ http ['header ' ] .= $ k . ': ' . $ v . "\r\n" ;
99+ $ url .= '& ' . http_build_query ($ query );
100+ }
101+
102+ $ ch = curl_init ();
103+ if ($ this ->http_prefix == 'https:// ' ) {
104+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , false );
105+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , false );
106+ }
107+ curl_setopt ($ ch , CURLOPT_URL , $ this ->http_prefix . $ url );
108+ curl_setopt ($ ch , CURLOPT_CUSTOMREQUEST , $ method ); //设置请求方式
109+ curl_setopt ($ ch , CURLOPT_HEADER , true );
110+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
111+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ header );
112+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ body );
113+ $ content = curl_exec ($ ch );
114+ $ status = curl_errno ($ ch );
115+ curl_close ($ ch );
116+ $ header = array ();
117+ if ($ status === 0 ) {
118+ if (($ index = strpos ($ content , "\r\n\r\n" )) !== false ) {
119+ $ header_body = substr ($ content , 0 , $ index );
120+ $ header_body = explode ("\r\n" , $ header_body );
121+ foreach ($ header_body as $ r ) {
122+ $ index = strpos ($ r , ': ' );
123+ if ($ index !== false ) {
124+ $ header [trim (substr ($ r , 0 , $ index ))] = trim (substr ($ r , $ index + 1 ));
125+ }
126+ }
54127 }
55128 }
56- $ http ['content ' ] = $ param ;
57- $ opts = array ('http ' => $ http );
58- $ context = stream_context_create ($ opts );
59- return @file_get_contents ($ url , false , $ context );
129+ return $ header ;
60130 }
61131}
0 commit comments