note 80442 added to function.curl-exec

From: Date: Wed, 16 Jan 2008 12:11:07 +0000
Subject: note 80442 added to function.curl-exec
Groups: php.notes 
Request: Send a blank email to php-notes+get-135137@lists.php.net to get a copy of this message
<?php class CurlRequest { private $ch; /** * Init curl session * * $params = array('url' => '', * 'host' => '', * 'header' => '', * 'method' => '', * 'referer' => '', * 'cookie' => '', * 'post_fields' => '', * ['login' => '',] * ['password' => '',] * 'timeout' => 0 * ); */ public function init($params) { $this->ch = curl_init(); $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.0.9) Gecko/20061206 Firefox/1.5.0.9'; $header = array( "Accept: text/xml,application/xml,application/xhtml+xml, text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", "Accept-Language: ru-ru,ru;q=0.7,en-us;q=0.5,en;q=0.3", "Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7", "Keep-Alive: 300"); if (isset($params['host']) && $params['host']) $header[]="Host: ".$host; if (isset($params['header']) && $params['header']) $header[]=$params['header']; @curl_setopt ( $this -> ch , CURLOPT_RETURNTRANSFER , 1 ); @curl_setopt ( $this -> ch , CURLOPT_VERBOSE , 1 ); @curl_setopt ( $this -> ch , CURLOPT_HEADER , 1 ); if ($params['method'] == "HEAD") @curl_setopt($this -> ch,CURLOPT_NOBODY,1); @curl_setopt ( $this -> ch, CURLOPT_FOLLOWLOCATION, 1); @curl_setopt ( $this -> ch , CURLOPT_HTTPHEADER, $header ); if ($params['referer']) @curl_setopt ($this -> ch , CURLOPT_REFERER, $params['referer'] ); @curl_setopt ( $this -> ch , CURLOPT_USERAGENT, $user_agent); if ($params['cookie']) @curl_setopt ($this -> ch , CURLOPT_COOKIE, $params['cookie']); if ( $params['method'] == "POST" ) { curl_setopt( $this -> ch, CURLOPT_POST, true ); curl_setopt( $this -> ch, CURLOPT_POSTFIELDS, $params['post_fields'] ); } @curl_setopt( $this -> ch, CURLOPT_URL, $params['url']); @curl_setopt ( $this -> ch , CURLOPT_SSL_VERIFYPEER, 0 ); @curl_setopt ( $this -> ch , CURLOPT_SSL_VERIFYHOST, 0 ); if (isset($params['login']) & isset($params['password'])) @curl_setopt($this -> ch , CURLOPT_USERPWD,$params['login'].':'.$params['password']); @curl_setopt ( $this -> ch , CURLOPT_TIMEOUT, $params['timeout']); } /** * Make curl request * * @return array 'header','body','curl_error','http_code','last_url' */ public function exec() { $response = curl_exec($this->ch); $error = curl_error($this->ch); $result = array( 'header' => '', 'body' => '', 'curl_error' => '', 'http_code' => '', 'last_url' => ''); if ( $error != "" ) { $result['curl_error'] = $error; return $result; } $header_size = curl_getinfo($this->ch,CURLINFO_HEADER_SIZE); $result['header'] = substr($response, 0, $header_size); $result['body'] = substr( $response, $header_size ); $result['http_code'] = curl_getinfo($this -> ch,CURLINFO_HTTP_CODE); $result['last_url'] = curl_getinfo($this -> ch,CURLINFO_EFFECTIVE_URL); return $result; } } ?> Example of use: <?php .......... try { $params = array('url' => 'http://www.google.com', 'host' => '', 'header' => '', 'method' => 'GET', // 'POST','HEAD' 'referer' => '', 'cookie' => '', 'post_fields' => '', // 'var1=value&var2=value 'timeout' => 20 ); $this->curl->init($params); $result = $this->curl->exec(); if ($result['curl_error']) throw new Exception($result['curl_error']); if ($result['http_code']!='200') throw new Exception("HTTP Code = ".$result['http_code']); if (!$result['body']) throw new Exception("Body of file is empty"); ............... } catch (Exception $e) { echo $e->getMessage(); } ?> ---- Server IP: 88.81.249.248 Probable Submitter: 78.109.18.10 ---- Manual Page -- http://www.php.net/manual/en/function.curl-exec.php Edit -- https://master.php.net/note/edit/80442 Del: integrated -- https://master.php.net/note/delete/80442/integrated Del: useless -- https://master.php.net/note/delete/80442/useless Del: bad code -- https://master.php.net/note/delete/80442/bad+code Del: spam -- https://master.php.net/note/delete/80442/spam Del: non-english -- https://master.php.net/note/delete/80442/non-english Del: in docs -- https://master.php.net/note/delete/80442/in+docs Del: other reasons-- https://master.php.net/note/delete/80442 Reject -- https://master.php.net/note/reject/80442 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#135137) next »