-
Notifications
You must be signed in to change notification settings - Fork 4
/
http.php
97 lines (90 loc) · 3.57 KB
/
http.php
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
93
94
95
96
97
<?php
/**
* botclasses.php - Bot classes for interacting with mediawiki.
*
* (c) 2009-2010 Chris G - http://en.wikipedia.org/wiki/User:Chris_G
* (c) 2010 Fale - http://en.wikipedia.org/wiki/User:Fale
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Developers (add your self here if you worked on the code):
* Chris - [[User:Chris_G]] - Wrote the whole code
* Fale - [[User:Fale]] - Code polish, docs
**/
/**
* This class is designed to provide a simplified interface to cURL which maintains cookies.
* @author Chris
**/
class http
{
protected $curl;
protected $cookiejar;
public $useragent;
public function __construct ()
{
$this->useragent = 'PHP cURL';
$this->curl = curl_init();
$this->cookiejar = '/tmp/http.cookies.'.dechex( rand( 0,99999999 ) ).'.dat';
touch( $this->cookiejar );
chmod( $this->cookiejar, 0600 );
curl_setopt( $this->curl, CURLOPT_COOKIEJAR,$this->cookiejar );
curl_setopt( $this->curl, CURLOPT_COOKIEFILE,$this->cookiejar );
}
/**
* Sends a GET request
* @param $url is the address of the page you are looking for
* @returns the page you asked for
**/
public function get ( $url )
{
curl_setopt( $this->curl, CURLOPT_URL, $url );
curl_setopt( $this->curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $this->curl, CURLOPT_MAXREDIRS, 10 );
curl_setopt( $this->curl, CURLOPT_HEADER, false );
curl_setopt( $this->curl, CURLOPT_HTTPGET, true );
curl_setopt( $this->curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $this->curl, CURLOPT_CONNECTTIMEOUT, 15 );
curl_setopt( $this->curl, CURLOPT_TIMEOUT, 40 );
curl_setopt( $this->curl, CURLOPT_USERAGENT, $this->useragent );
return curl_exec( $this->curl );
}
/**
* Sends a POST request
* @param $url is the address of the page you are looking for
* @param $data is the post data.
* @returns the page you asked for
**/
public function post ( $url, $data )
{
curl_setopt( $this->curl, CURLOPT_URL, $url );
curl_setopt( $this->curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $this->curl, CURLOPT_MAXREDIRS, 10 );
curl_setopt( $this->curl, CURLOPT_HEADER, false );
curl_setopt( $this->curl, CURLOPT_POST, true );
curl_setopt( $this->curl, CURLOPT_POSTFIELDS, $data );
curl_setopt( $this->curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $this->curl, CURLOPT_CONNECTTIMEOUT, 15 );
curl_setopt( $this->curl, CURLOPT_TIMEOUT, 40 );
curl_setopt( $this->curl, CURLOPT_USERAGENT, $this->useragent );
curl_setopt( $this->curl, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
return curl_exec( $this->curl );
}
public function __destruct ()
{
curl_close( $this->curl );
@unlink( $this->cookiejar );
}
}
?>