PHP7中文手册2018 带注释 最新chm版
- <?php
- /**
- * CurlTest
- */
- class Curl {
- private $_ch;
- private $response;
- private function exec($url)
- {
- $this->setOption(CURLOPT_URL, $url);
- $this->response = curl_exec($this->_ch);
- if (!curl_errno($this->_ch)) {
- $header_size = curl_getinfo($this->_ch, CURLINFO_HEADER_SIZE);
- $body = substr($this->response, $header_size);
- return $body;
- } else {
- throw new CException(curl_error($this->_ch));
- }
- return false;
- }
- public function put($url, $data, $params = array())
- {
- // write to memory/temp
- $f = fopen('php://temp', 'rw+');
- fwrite($f, $data);
- rewind($f);
- $this->setOption(CURLOPT_PUT, true);
- $this->setOption(CURLOPT_INFILE, $f);
- $this->setOption(CURLOPT_INFILESIZE, strlen($data));
- return $this->exec($this->buildUrl($url, $params));
- }
- public function setOption($option, $value)
- {
- curl_setopt($this->_ch, $option, $value);
- return $this;
- }
- public function buildUrl($url, $data = array())
- {
- $parsed = parse_url($url);
- isset($parsed['query']) ? parse_str($parsed['query'], $parsed['query']) : $parsed['query'] = array();
- $params = isset($parsed['query']) ? array_merge($parsed['query'], $data) : $data;
- $parsed['query'] = ($params) ? '?' . http_build_query($params) : '';
- if (!isset($parsed['path'])) {
- $parsed['path']='/';
- }
- $parsed['port'] = isset($parsed['port'])?':'.$parsed['port']:'';
- return $parsed['scheme'].'://'.$parsed['host'].$parsed['port'].$parsed['path'].$parsed['query'];
- }
- public function get($url, $params = array())
- {
- $this->setOption(CURLOPT_HTTPGET, true);
- return $this->exec($this->buildUrl($url, $params));
- }
- }
- $c = new Curl();
- $res = $c->get("http://www.kuitao8.com");
- print_r($res);
转载请注明:谷谷点程序 » php curl简单的类