Спойлер (Отобразить)
PHP:
скопировать код в буфер обмена
скопировать код в буфер обмена
- <?PHP
- /**
- * User: panoptik
- * Date: 7/10/14
- * Time: 4:35 PM
- */
- class CurlRequest
- {
- public $url;
- public $cookieFile;
- {
- $this->url = $params['url'];
- }
- $this->cookieFile = $params['cookieFile'];
- }
- }
- /**
- * send post request and return result
- * @param $url
- * @param $fields
- * @param bool $ajax
- * @param bool $jsonDecode
- * @throws Exception
- * @return mixed
- */
- public function sendPost($url, $fields, $ajax=false, $jsonDecode=false)
- {
- 'ajax' => $ajax,
- 'json' => $jsonDecode,
- ), $fields);
- }
- /**
- * send get request
- * @param $url
- * @param bool $ajax
- * @param bool $jsonDecode
- * @return mixed
- */
- public function sendGet($url, $ajax=false, $jsonDecode=false)
- {
- 'ajax'=>$ajax,
- 'json' => $jsonDecode,
- ));
- }
- /**
- * @param string $url
- * @return resource curl
- */
- public function getCurl($url = '')
- {
- $url = $this->url . $url;
- 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36');
- return $ch;
- }
- /**
- * make curl request with specified type and options
- * possible ajax request (need to set $options['ajax'])
- * possible json decode (need to set $options['json'])
- *
- * @param $url
- * @param $type
- * @param array $options
- * @param array $data additional data will be send with post
- * @return mixed
- * @throws Exception
- */
- {
- $ch = $this->getCurl($url);
- $options[CURLOPT_POST] = 1;
- $options[CURLOPT_POSTFIELDS] = $data;
- }
- // prepare headers
- if($isAjax) {
- $headers[] = "X-Requested-With: XMLHttpRequest";
- }
- if($isJson) {
- $headers[] = "Accept: application/json; charset=utf-8";
- }
- $options[CURLOPT_HTTPHEADER] = $headers;
- $options[CURLINFO_HEADER_OUT] = 1;
- // set other request options if set
- }
- if($isJson) {
- $json = CJSON::decode($result);
- if(!$json) {
- throw new Exception('Cannot decode json. Wrong response given ' . PHP_EOL . $result);
- }
- return $json;
- }
- return $result;
- }
- }