使用方式
~~~
$keyword="自媒體";
$keyword = urlencode($keyword);
$catchUrl = "https://www.toutiao.com/search_content/?offset=0&format=json&keyword={$keyword}&autoload=true&count=20&cur_tab=1";
$h=new JqHttp();
$htmlcode = $h->ihttp_get($catchUrl);
$htmlcode = $htmlcode['content'];
$htmlcode = base64_encode($htmlcode);
$htmlcode = $h->ihttp_post('http://we7cc.csdn123.net/toutiao_news/now.catch.php', array('htmlcode' => $htmlcode));
$htmlcode = $htmlcode['content'];
$htmlcode = preg_replace('/^\s+|\s+$/', '', $htmlcode);
$htmlcode = base64_decode($htmlcode);
$linkArr = unserialize($htmlcode);
print_r($linkArr);
~~~
類實現
~~~
<?php
namespace jqstu;
class JqHttp
{
var $curlconf=[
'TIMEOUT'=>60,
'useCert'=>false,
'SSLCERT_PATH'=>'',
'SSLKEY_PATH'=>'',
];
public function __construct($conf=[])
{
$this->curlconf=array_merge($this->curlconf, $conf);
}
/**
* @param $url
* @return array
* @throws \Exception
*/
public function ihttp_get($url)
{
return self::ihttp_request($url);
}
/**
* @param $url
* @param $data
* @return array
* @throws \Exception
*/
public function ihttp_post($url, $data) {
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
return self::ihttp_request($url, $data, $headers);
}
/**
* @param $url
* @param string $post
* @param array $extra
* @param int $timeout
* @return array
* @throws \Exception
*/
public function ihttp_request($url, $post = '', $extra = array())
{
$timeout=$this->curlconf['TIMEOUT'];
if (function_exists('curl_init') && function_exists('curl_exec') && $timeout > 0) {
$ch = self::ihttp_build_curl($url, $post, $extra, $timeout);
$data = curl_exec($ch);
$status = curl_getinfo($ch);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
if ($errno || empty($data)) {
Jqutil::JqException($error,$errno);
} else {
return self::ihttp_response_parse($data);
}
}
$urlset = self::ihttp_parse_url($url, true);
if (!empty($urlset['ip'])) {
$urlset['host'] = $urlset['ip'];
}
$body = self::ihttp_build_httpbody($url, $post, $extra);
if ($urlset['scheme'] == 'https') {
$fp = self::ihttp_socketopen('ssl://' . $urlset['host'], $urlset['port'], $errno, $error);
} else {
$fp = self::ihttp_socketopen($urlset['host'], $urlset['port'], $errno, $error);
}
stream_set_blocking($fp, $timeout > 0 ? true : false);
stream_set_timeout($fp, ini_get('default_socket_timeout'));
if (!$fp) {
Jqutil::JqException($error,1);
} else {
fwrite($fp, $body);
$content = '';
if($timeout > 0) {
while (!feof($fp)) {
$content .= fgets($fp, 512);
}
}
fclose($fp);
return self::ihttp_response_parse($content, true);
}
}
private function ihttp_socketopen($hostname, $port = 80, &$errno, &$errstr, $timeout = 15) {
$fp = '';
if(function_exists('fsockopen')) {
$fp = @fsockopen($hostname, $port, $errno, $errstr, $timeout);
} elseif(function_exists('pfsockopen')) {
$fp = @pfsockopen($hostname, $port, $errno, $errstr, $timeout);
} elseif(function_exists('stream_socket_client')) {
$fp = @stream_socket_client($hostname.':'.$port, $errno, $errstr, $timeout);
}
return $fp;
}
private function ihttp_build_httpbody($url, $post, $extra) {
$urlset = self::ihttp_parse_url($url, true);
if ($urlset) {
return $urlset;
}
if (!empty($urlset['ip'])) {
$extra['ip'] = $urlset['ip'];
}
$body = '';
if (!empty($post) && is_array($post)) {
$filepost = false;
$boundary = Jqutil::random(40);
foreach ($post as $name => &$value) {
if ((is_string($value) && substr($value, 0, 1) == '@') && file_exists(ltrim($value, '@'))) {
$filepost = true;
$file = ltrim($value, '@');
$body .= "--$boundary\r\n";
$body .= 'Content-Disposition: form-data; name="'.$name.'"; filename="'.basename($file).'"; Content-Type: application/octet-stream'."\r\n\r\n";
$body .= file_get_contents($file)."\r\n";
} else {
$body .= "--$boundary\r\n";
$body .= 'Content-Disposition: form-data; name="'.$name.'"'."\r\n\r\n";
$body .= $value."\r\n";
}
}
if (!$filepost) {
$body = http_build_query($post, '', '&');
} else {
$body .= "--$boundary\r\n";
}
}
$method = empty($post) ? 'GET' : 'POST';
$fdata = "{$method} {$urlset['path']}{$urlset['query']} HTTP/1.1\r\n";
$fdata .= "Accept: */*\r\n";
$fdata .= "Accept-Language: zh-cn\r\n";
if ($method == 'POST') {
$fdata .= empty($filepost) ? "Content-Type: application/x-www-form-urlencoded\r\n" : "Content-Type: multipart/form-data; boundary=$boundary\r\n";
}
$fdata .= "Host: {$urlset['host']}\r\n";
$fdata .= "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1\r\n";
if (function_exists('gzdecode')) {
$fdata .= "Accept-Encoding: gzip, deflate\r\n";
}
$fdata .= "Connection: close\r\n";
if (!empty($extra) && is_array($extra)) {
foreach ($extra as $opt => $value) {
if (!Jqutil::strexists($opt, 'CURLOPT_')) {
$fdata .= "{$opt}: {$value}\r\n";
}
}
}
if ($body) {
$fdata .= 'Content-Length: ' . strlen($body) . "\r\n\r\n{$body}";
} else {
$fdata .= "\r\n";
}
return $fdata;
}
/**
* @param $url
* @param $post
* @param $extra
* @param $timeout
* @return resource
* @throws \Exception
*/
private function ihttp_build_curl($url, $post, $extra, $timeout)
{
if (!function_exists('curl_init') || !function_exists('curl_exec')) {
Jqutil::JqException("curl擴展未開啟");
}
$urlset = self::ihttp_parse_url($url);
// print_r($urlset);
if (!empty($urlset['ip'])) {
$extra['ip'] = $urlset['ip'];
}
$ch = curl_init();
if (!empty($extra['ip'])) {
$extra['Host'] = $urlset['host'];
$urlset['host'] = $extra['ip'];
unset($extra['ip']);
}
if ($urlset['port']=80){
$urlset['port']='';
}
if(empty($urlset['query'])){
$urlset['query']='';
}
curl_setopt($ch, CURLOPT_URL, $urlset['scheme'] . '://' . $urlset['host'] . $urlset['port'] . $urlset['path'] . $urlset['query']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
@curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
if ($post) {
if (is_array($post)) {
$filepost = false;
foreach ($post as $name => &$value) {
if (version_compare(phpversion(), '5.5') >= 0 && is_string($value) && substr($value, 0, 1) == '@') {
$post[$name] = new \CURLFile(ltrim($value, '@'));
}
if ((is_string($value) && substr($value, 0, 1) == '@') || (class_exists('CURLFile') && $value instanceof CURLFile)) {
$filepost = true;
}
}
if (!$filepost) {
$post = http_build_query($post);
}
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
}
$proxy=array('host'=>'127.0.0.1','auth'=>'');
if (!empty($proxy['host']) || !empty($proxy['auth'])){
$urls = parse_url($proxy['host']);
if (!empty($urls['host'])){
curl_setopt($ch, CURLOPT_PROXY, "{$urls['host']}:{$urls['port']}");
$proxytype = 'CURLPROXY_' . strtoupper($urls['scheme']);
if (!empty($urls['scheme']) && defined($proxytype)) {
curl_setopt($ch, CURLOPT_PROXYTYPE, constant($proxytype));
} else {
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
}
if (!empty($proxy['auth'])) {
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy['auth']);
}
}
}
if (defined('CURL_SSLVERSION_TLSv1')) {
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
//安全證書檢查
$curlconf=$this->curlconf;
if ($curlconf['useCert'] && !empty($curlconf['SSLCERT_PATH']) || !empty($curlconf['SSLKEY_PATH'])){
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//嚴格校驗
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT, $curlconf['SSLCERT_PATH']);
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY,$curlconf['SSLKEY_PATH']);
}else{
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
}
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1');
if (!empty($extra) && is_array($extra)) {
$headers = array();
foreach ($extra as $opt => $value) {
if (Jqutil::strexists($opt, 'CURLOPT_')) {
curl_setopt($ch, constant($opt), $value);
} elseif (is_numeric($opt)) {
curl_setopt($ch, $opt, $value);
} else {
$headers[] = "{$opt}: {$value}";
}
}
if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
}
return $ch;
}
/**
* @param $url
* @param bool $set_default_port
* @return mixed
* @throws \Exception
*/
private function ihttp_parse_url($url, $set_default_port = false)
{
if (empty($url)) {
Jqutil::JqException("url參數沒有填寫");
}
$urlset = parse_url($url);
if (!empty($urlset['scheme']) && !in_array($urlset['scheme'], array('http', 'https'))) {
Jqutil::JqException("只能使用 http 及 https 協議");
}
if (!empty($urlset['query'])) {
$urlset['query'] = "?{$urlset['query']}";
}
if (Jqutil::strexists($url, 'https://') && !extension_loaded('openssl')) {
if (!extension_loaded("openssl")) {
Jqutil::JqException("請開啟您PHP環境的openssl");
}
}
if ($set_default_port && empty($urlset['port'])) {
$urlset['port'] = $urlset['scheme'] == 'https' ? '443' : '80';
}
if (empty($urlset['host'])) {
$siteroot = $_SERVER['SERVER_NAME'];
$current_url = parse_url($siteroot);
$urlset['host'] = $current_url['host'];
$urlset['scheme'] = $current_url['scheme'];
$urlset['path'] = $current_url['path'] . '/' . str_replace('./', '', $urlset['path']);
$urlset['ip'] = '127.0.0.1';
} elseif (!self::ihttp_allow_host($urlset['host'])) {
Jqutil::JqException('host 非法');
}
if (array_key_exists("port",$urlset)) {
$urlset['port']=$urlset['port'];
} else {
$urlset['port']=80;
}
return $urlset;
}
/**
* @param $data
* @param bool $chunked
* @return array
*/
private function ihttp_response_parse($data, $chunked = false){
$rlt = array();
$headermeta = explode('HTTP/', $data);
if (count($headermeta) > 2) {
$data = 'HTTP/' . array_pop($headermeta);
}
$pos = strpos($data, "\r\n\r\n");
$split1[0] = substr($data, 0, $pos);
$split1[1] = substr($data, $pos + 4, strlen($data));
$split2 = explode("\r\n", $split1[0], 2);
preg_match('/^(\S+) (\S+) (.*)$/', $split2[0], $matches);
$rlt['code'] = $matches[2];
$rlt['status'] = $matches[3];
$rlt['responseline'] = $split2[0];
$isgzip = false;
$ischunk = false;
$header = explode("\r\n", $split2[1]);
foreach ($header as $v){
$pos = strpos($v, ':');
$key = substr($v, 0, $pos);
$value = trim(substr($v, $pos + 1));
$rlt['headers'][$key] = $value;
if(!$isgzip && strtolower($key) == 'content-encoding' && strtolower($value) == 'gzip') {
$isgzip = true;
}
if(!$ischunk && strtolower($key) == 'transfer-encoding' && strtolower($value) == 'chunked') {
$ischunk = true;
}
}
if($chunked && $ischunk) {
$rlt['content'] = self::ihttp_response_parse_unchunk($split1[1]);
} else {
$rlt['content'] = $split1[1];
}
$rlt['content'] = $split1[1];
if($isgzip && function_exists('gzdecode')) {
$rlt['content'] = gzdecode($rlt['content']);
}
if($rlt['code'] == '100') {
return self::ihttp_response_parse($rlt['content']);
}
return $rlt;
}
/**
* @param null $str
* @return bool|null|string
*/
private function ihttp_response_parse_unchunk($str = null) {
if(!is_string($str) or strlen($str) < 1) {
return false;
}
$eol = "\r\n";
$add = strlen($eol);
$tmp = $str;
$str = '';
do {
$tmp = ltrim($tmp);
$pos = strpos($tmp, $eol);
if($pos === false) {
return false;
}
$len = hexdec(substr($tmp, 0, $pos));
if(!is_numeric($len) or $len < 0) {
return false;
}
$str .= substr($tmp, ($pos + $add), $len);
$tmp = substr($tmp, ($len + $pos + $add));
$check = trim($tmp);
} while(!empty($check));
unset($tmp);
return $str;
}
/**
* @param $host
* @return bool
*/
private function ihttp_allow_host($host)
{
if (Jqutil::strexists($host, '@')) {
return false;
}
$pattern = "/^(10|172|192|127)/";
if (preg_match($pattern, $host)) {
return false;
}
return true;
}
}
~~~
- 序言
- 基礎知識
- thinkphp基礎知識
- Thinkphp5CURD
- 數據庫創建
- 數據庫刪除
- 數據庫更新
- 數據庫查詢
- thinkphp5控制器
- 空操作空控制器
- 控制器基類
- 請求信息
- 行為和鉤子
- thinkphp5路由設置
- 變量路由
- 常用方法清單
- 環境搭建
- lnmp
- 升級php
- window環境
- Thinkphp小案例
- 分類管理
- 數據庫設計
- 模型
- 控制器
- 視圖
- 文件上傳
- 上傳接口
- 視圖
- 表單提交
- 視圖設計
- 控制器
- 權限控制
- 案例解釋說明
- 登錄驗證
- Laravel5.3登錄模式
- redis使用
- 一鍵安裝
- 程序設計與實現
- 頁面設計
- 功能設計
- 安裝功能實際
- 函數庫
- 配置文件
- 清除緩存
- 狀態值修改
- 數據庫備份還原
- controller.php
- common.php
- index.html
- importlist.html
- 完整的增刪查改
- 查詢語句
- 多語言支持
- JpGraph圖表類庫
- 微信支付
- payBase.php
- Order.php
- Oauth.php
- Jspay.php
- 下載遠程地址中的圖片
- URL重寫隱藏入口文件
- 圖片水印
- 整合百度編輯器
- Ueditor
- ueditor完整配置項
- 配置信息常見的方式
- HTTP 斷點續傳(PHP實現)
- layui.upload上傳文件或圖片
- QQ微信域名防封 預防域名封禁 強制跳轉至瀏覽器
- 蜘蛛篇
- 超簡單實現php谷歌驗證
- 采集金山詞霸每日一句
- think-swoole
- 原生PHP小案例
- 查詢修改數據庫
- mysql支付回調源碼
- pdo連接微信退款
- 前端小案例
- html快捷查詢
- layui經驗總結
- layui 表單增強插件
- Vue列表Ajax實戰教程
- PHP基礎
- 類的自動載入
- php基礎函數- 字符串函數
- php基礎函數-數學函數
- php基礎函數-數組函數
- PHP常見排序算法學習
- 請求第三方
- 從網絡下載文件
- 檢查網站是否宕機
- file_get_contents
- 算法
- php 抽獎算法(適合九宮格和大轉盤)
- 自己動手豐衣足食
- 入口文件
- start.php
- app.php
- load.php
- route.php
- JqHttp
- Jqfile
- Jqutil
- pdo連接數據庫類
- 常見的php類
- php數據接口類
- 生成多層樹狀下拉選框的工具模型
- 上傳下載類
- 微信用戶相關類
- Zip壓縮類
- 列表樹生成工具類
- 日期時間操作類
- 文件及文件夾處理類
- 字符串處理類
- php守護進程類
- RSA算法類
- php支持中英文的加密解密類
- CURL多線程請求
- 通用數據庫操作類
- 緩存類
- cookie類
- 常見的驗證方法
- 隨機密鑰
- 日志Log
- php-redis 操作類 封裝
- OpensslRsa 加密、解密、簽名、驗簽類
- 模板輸出類
- 發送郵件
- 封裝的mysqli類
- PHP時間段分割類庫
- PHP apk解包識版本號信息和ipa包信息
- 訪問客戶端信息
- http請求
- PHP 無數據庫讀寫配置文件
- 自己動手寫一個jwt類
- php實現對圖片對稱加解密(適用身份證加密等場景)
- 常見php函數
- 無限分類
- 獲取文章圖片
- 加密解密
- JSON數據輸出(適合在tp中)
- 刪除目錄和文件
- 判斷是否為手機訪問
- 獲取客戶端真實IP
- 隨機生成ip地址
- 字符串與二進制進行轉換
- 對數組進行排序
- 格式化字節大小
- 時間戳格式化
- 獲取數據的所有子孫數據的id值
- 取得視頻文件的縮略圖
- 圖片裁剪函數
- 按照每過0:00算一天
- 下載文件
- PHP隨機密碼生成
- 判斷數字大小
- 報文組成
- 通過ip定位城市
- PDO方式連接MySQL數據庫
- 數組與xml
- php字符串處理函數
- 判斷是否ajax提交
- 生成概率,用于抽獎
- 斷點續傳
- PHP使用星號替代用戶名手機和郵箱
- 獲取毫秒級別的時間戳
- php日志函數
- 隨機顏色生成器
- 時間差異計算函數
- 黑名單過濾
- 常見PHP 正則表達式
- php獲取瀏覽器類型
- 郵件發送
- 獲取qq昵稱
- 正則獲取手機號歸屬地
- 判斷是否是移動客戶端 移動設備
- gbk和utf8編碼自動識別方法
- 人性化時間顯示
- 請求API接口
- 數據庫備份
- PHP并發下安全讀寫文件函數
- PHP讀取exe軟件版本號
- PHP為任意頁面設置訪問密碼
- PHP利用百度當圖床
- 秒/分鐘/小時前
- 常見的js函數
- 短信驗證函數
- 上下收縮菜單
- jQuery 樹插件zTree
- 頁面刷新跳轉
- jquery導出報表
- js實現定時效果
- 獲取當前經緯度
- JQuery實現圖片大小自適應
- 網站運行時間
- 判斷瀏覽器類型
- 百度推送
- js對指定數據進行排序
- 常見工具方法
- JSPinyin
- 技術相關文章
- 高級PHP工程師所應該具備哪些技能
- 最簡潔的PHP程序員學習路線及建議
- 優化PHP代碼的一些建議
- TP5性能優化建議
- 程序猿專用代碼注釋:佛祖保佑,永無BUG
- 一組匹配中國大陸手機號碼的正則表達式
- Apache/Nginx/PHP服務器反爬蟲代碼大全
- 番外
- 配置shadowsocks服務端
- python
- go
- 如何在1分鐘內黑掉任何網站!
- 百度貼吧敏感詞
- 貼吧手工養號發帖教程
- 搞笑的注釋代碼
- Heroku