-
Notifications
You must be signed in to change notification settings - Fork 0
/
mw_api_post.php
151 lines (119 loc) · 4.17 KB
/
mw_api_post.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/******************************************************************************
Copyright 2008-2010 Christopher L. Stafford
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 3 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, see <http://www.gnu.org/licenses/>.
******************************************************************************/
if( defined('API_LOGIN_COOKIE_FILE') == false )
{
//no filename defined, so set our own
define('API_LOGIN_COOKIE_FILE', 'login.cookie');
}
/** func:
mw_api_post
desc:
takes a assoc array of params, and posts to the SITE's api.php
param:
site [string]
used to build url to api.php
param_array [array]
assoc array of parameters to send
note: value of will be changed to 1
login [bool][optional=true]
flag to send login credentials
results:
[false]
got non php data structure back
[array]
what ye asked for
require:
nothing? does own curl code
**/
function mw_api_post($site, $param_array, $login=true)
{
//ALWAYS make sure its in php format, even if they send it in another way
$param_array['format'] = 'php';
//loop over passed params, fix nulls
foreach($param_array as $var=>$val)
{
if( $val === null)
{
$param_array[$var] = '1';
}
}
//hook for debugging
global $LAST_POST_PARAM;
$LAST_POST_PARAM = $param_array;
/***************************************************************************/
//create cURL object
$ch = curl_init();
/***************************************************************************/
// URL stuff
//build
$url = "http://{$site}/api.php";
//hook
global $LAST_POST_URL;
$LAST_POST_URL = $url;
//use
curl_setopt($ch, CURLOPT_URL, $url);
/***************************************************************************/
// set other appropriate options
//check to to see if we need to send creds with message (default is ON)
if( $login )
{
if( file_exists(API_LOGIN_COOKIE_FILE) )
{
curl_setopt($ch, CURLOPT_COOKIEFILE, API_LOGIN_COOKIE_FILE);
}
}
curl_setopt($ch, CURLOPT_HEADER, false); //no headers, just body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //no print, just return
curl_setopt($ch, CURLOPT_POST, true); //send as post, not get
curl_setopt($ch, CURLOPT_POSTFIELDS, $param_array); //what to send
#swap comment to use this if you need to post a param with a string starting with a @, and its NOT an upload
#curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param_array));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //follow if redirected
/***************************************************************************/
// set user agent
$useragent="FzyBot/0.0";
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
/***************************************************************************/
// have payload, have options, so... send!
//setup hooks and clear them
global $LAST_POST_RAW, $LAST_POST_CGI;
$LAST_POST_RAW = null;
$LAST_POST_CGI = null;
//DOEET
$raw = curl_exec($ch);
$cgi = curl_getinfo($ch); //quick, capture transaction data
//fill debug data
$LAST_POST_RAW = $raw;
$LAST_POST_CGI = $cgi;
/***************************************************************************/
// close cURL resource
curl_close($ch);
/***************************************************************************/
// use uglee methods to check what we got back
//do we have a content_type to check?
if( empty($cgi['content_type']) )
{
// no c_type to check
return false;
}
//ok, there is a content type, is it the PHP encoded sig?
if( strpos($cgi['content_type'], 'application/vnd.php.serialized') === false )
{
// wrong c_type
return false;
}
//yup, return unwrapped data array
return unserialize($raw);
}