function getHtml($url) { $curlObj = new Curl(); $curlObj->setUrl($url); $html = $curlObj->run(); //标题 $p = '/<h1>(.*?)<\\/h1>/'; preg_match($p, $html, $match); $title = isset($match[1]) ? $match[1] : ''; //echo $title; //日期 $p = '/<div class="time">(.*?)<\\/div>/'; preg_match($p, $html, $match); $time = isset($match[1]) ? $match[1] : 0; if (!empty($time)) { $time = str_replace(array('年', '月'), '', $time); $time = str_replace('日', '', $time); $time = strtotime($time); } //echo $time; //摘要 $p = '/<div class="b-review">(.*?)<\\/div>/'; preg_match($p, $html, $match); $short = isset($match[1]) ? $match[1] : ''; //echo $short; //内容 $p = '/<!--文章主体-->([\\s|\\S]*?)<!--原文标题-->/'; preg_match($p, $html, $match); $content = isset($match[1]) ? $match[1] : ''; //echo $content; //分页 while (true) { $p = '/<a href="[^<]*?(_(\\d+)\\.html)" target="_self" class="page-btn">下一页<\\/a>/'; preg_match($p, $html, $match); $next = isset($match[1]) ? $match[1] : ''; if ($next) { $url = str_replace('.html', $match[1], $url); $curlObj->setUrl($url); $html = $curlObj->run(); //内容 $p = '/<!--文章主体-->([\\s|\\S]*?)<!--原文标题-->/'; preg_match($p, $html, $match); $content .= isset($match[1]) ? $match[1] : ''; } else { break; } } // echo str_replace(" ", "\n", strip_tags($content));die(); $data = array('title' => $title, 'time' => $time, 'short' => $short, 'content' => str_replace(" ", "\n", strip_tags($content))); echo json_encode($data); }
function post($url, $data) { $curlObj = new Curl(); $curlObj->setUrl($url); $curlObj->setPost($data); return $curlObj->run(); }
/** * @param $url * @param $filePath * * @return $this * @throws Exception */ public function download($url, $filePath) { $fp = fopen($filePath, 'w+'); $this->curl->setUrl($url)->setTimeout(50)->setFile($fp)->followLocation()->execute(); fclose($fp); return $this; }
/** * static proxy create */ public static function create($url) { $curl = new Curl($url); if ($url) { $curl->setUrl($url); } return $curl; }
public function post($url, $getParameters = array(), $postParameters, $isJson = false, $curlOptions = array()) { $curl = new Curl(); $curl->setUrl($url, $getParameters); $curl->setMethod(true); if ($isJson) { $postParameters = json_encode($postParameters); $curl->addOption('HTTP_HEADER', array('Content-Type: application/json')); } $curl->setPostParameters($postParameters); foreach ($curlOptions as $key => $value) { $curl->addOption($key, $value); } return $this->send($curl, $isJson); }
public function curl_multi_add_handle() { // create both cURL resources $ch1 = new Curl(); $ch2 = new Curl(); // set URL and other appropriate options $ch1->setUrl("http://www.example.com/")->includeHeader(false); $ch2->setUrl("http://www.php.net/")->includeHeader(false); //create the multiple cURL handle $mh = new MultiCurl(); //add the two handles $mh->add($ch1)->add($ch2); $running = null; //execute the handles do { curl_multi_exec($mh->getMultiHandle(), $running); } while ($running > 0); //close all the handles unset($mh); }
function getId($unique) { $url = "http://app.qichacha.com/enterprises/new/getShareURL?unique=" . $unique; $tempIp = rand(1, 255) . '.' . rand(1, 255) . '.' . rand(1, 255) . '.' . rand(1, 255); $header = array("CLIENT-IP:{$tempIp}", "X-FORWARDED-FOR:{$tempIp}"); $curl = new Curl(); $curl->setUrl($url); $resStr = $curl->run(); preg_match('/share\\/(.*?)"/', $resStr, $match); if (isset($match[1])) { return $match[1]; } else { return ''; } }
/** * Add Search * * @access public * @param $url * @param $data * * @return object */ public function addSearch($url, $data = array()) { if (is_array($url)) { $data = $url; $url = $this->baseUrl; } $curl = new Curl(); $curl->setUrl($url); $curl->setOpt(CURLOPT_CUSTOMREQUEST, 'SEARCH'); $put_data = $curl->buildPostData($data); if (is_string($put_data)) { $curl->setHeader('Content-Length', strlen($put_data)); } $curl->setOpt(CURLOPT_POSTFIELDS, $put_data); $this->queueHandle($curl); return $curl; }
function getArticleInfo($name) { global $db; $curl = new Curl(); $url = "http://www.jianshu.com/p/{$name}"; $ip = rand(1, 255) . '.' . rand(1, 255) . '.' . rand(1, 255) . '.' . rand(1, 255); $curl->setUrl($url); $head = array("CLIENT-IP:{$ip}", "X-FORWARDED-FOR:{$ip}"); $curl->setHttpHeader($head); $html = $curl->run(); // echo $url,"\r\n"; // echo $html; $p = "/<script type='application\\/json' data-name='note'>\\s*(.*?)\\s*<\\/script>/"; preg_match($p, $html, $match); $arr = isset($match[1]) ? json_decode($match[1], true) : array(); $data['read_num'] = isset($arr['views_count']) ? $arr['views_count'] : 0; $data['comment_num'] = isset($arr['comments_count']) ? $arr['comments_count'] : 0; $data['like_num'] = isset($arr['likes_count']) ? $arr['likes_count'] : 0; $data['image_url'] = isset($arr['image_url']) ? $arr['image_url'] : 0; //文章详情 $p = '/<div class="show-content">([\\s\\S]*?)<\\/div>\\s*<\\/div>\\s*<\\/div>\\s*<div class="visitor_edit"/'; preg_match($p, $html, $match); // print_r($match); $data['content'] = isset($match[1]) ? $match[1] : ''; //评论用户 $data['comment_user'] = array(); $p = "/<script type='application\\/json' data-name='uuid'>\\s*(.*?)\\s*<\\/script>/"; preg_match($p, $html, $match); // print_r($match); $arr = isset($match[1]) ? json_decode($match[1], true) : array(); $uuid = $arr['uuid']; if (!empty($uuid)) { $curl->setUrl("http://www.jianshu.com/notes/cae7cda41db4/mark_viewed.json"); $curl->setPost($arr); $html = $curl->run(); $temp = json_decode($html, true); //print_r($temp); if (is_array($temp['likes']) && !empty($temp['likes'])) { foreach ($temp['likes'] as $value) { $data['comment_user'][] = $value['user']['slug']; } } } return $data; }