<?php
class MiniCurl {
private $ch;
function __construct($url="") {
if(strlen($url)>3);
$this->init($url);
}
public function init($url){
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_URL, $url);
curl_setopt($this->ch, CURLOPT_HEADER, TRUE);
// curl_setopt($this->ch, CURLOPT_NOBODY, TRUE);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
// curl_setopt($this->ch, CURLOPT_HTTPHEADER, array(
// 'X-Apple-Tz: 0',
// 'X-Apple-Store-Front: 143444,12'
// ));
curl_setopt($this->ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($this->ch, CURLOPT_COOKIEJAR, 'cookie.txt');
}
public function setHeader($headerKey, $headerText){
curl_setopt($this->ch,$headerKey,$headerText);
}
public function setHeaderJson(){
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json')
);
return $this;
}
public function getData() {
$response = curl_exec ( $this->ch );
if (curl_errno($this->ch)) {
$error_msg = curl_error($this->ch);
throw new Exception($error_msg);
}
if ($response === false) {
throw new Exception("error hap");
return 'Curl-problem: ' . curl_error ( $this->ch );
}
else
{
if (preg_match ( '/Location: (.+)/i', $response, $matches )) {
throw new Exception("error hap");
curl_setopt($this->ch, CURLOPT_HEADER, FALSE);
curl_setopt ( $this->ch, CURLOPT_URL, trim($matches [1]) );
return $this->getData();
}
else{
return $response;
}
}
}
public function postData($data_json){
curl_setopt($this->ch, CURLOPT_POST, 1);
// curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($this->ch, CURLOPT_POSTFIELDS,$data_json);
if($response = curl_exec($this->ch) === false)
{
echo 'Curl-Problem: ' . curl_error($this->ch);
}
$result = curl_exec($this->ch);
$header_size = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE);
$header = substr($result , 0, $header_size);
$body = substr($result , $header_size);
$response["body"] =$body;
$response["header"]=$header;
return $response;
}
function __destruct() {
curl_close($this->ch);
}
}