function request ch url data NULL curl_setopt ch CURLOPT_URL url if da

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
function request($ch, $url, $data = NULL) {
curl_setopt($ch, CURLOPT_URL, $url);
if ($data !== NULL) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$raw = curl_exec($ch);
$headers_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
// echo "[".curl_getinfo($ch, CURLINFO_HTTP_CODE)."] ".$url." ($headers_size:".strlen($raw).")\n";
curl_setopt($ch, CURLOPT_POST, false);
$res = new StdClass;
$res->type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$res->mime = strtolower(explode(";", $res->type)[0]);
$res->code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$res->headers = substr($raw, 0, $headers_size);
$res->body = substr($raw, $headers_size);
return $res;
}