reasonableframework/helper/webpagetool.php

92 lines
2.4 KiB
PHP
Raw Normal View History

2018-02-26 06:05:25 +00:00
<?php
/**
* @file webpagetool.php
* @date 2018-02-26
* @author Go Namhyeon <gnh1201@gmail.com>
* @brief WebPageTool helper
*/
2018-02-26 06:10:06 +00:00
if(!function_exists("get_web_page")) {
2018-02-26 06:05:25 +00:00
function get_web_page($url, $method="get", $data=array(), $proxy="", $ua="", $ct_out=45, $t_out=45) {
$options = array(
CURLOPT_PROXY => "", // set proxy server
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_USERAGENT => "", // name of client
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => $ct_out, // time-out on connect
CURLOPT_TIMEOUT => $c_out, // time-out on response
);
if(!empty($ua)) {
$options[CURLOPT_USERAGENT] = $ua;
} else {
2018-02-26 06:08:11 +00:00
$options[CURLOPT_USERAGENT] = "2018 ReasonableFramework;https://github.com/gnh1201/reasonableframework";
2018-02-26 06:05:25 +00:00
}
if(!empty($proxy)) {
$options[CURLOPT_PROXY] = $proxy;
}
if($method == "post" && count($data) > 0) {
$options[CURLOPT_POST] = 1;
$options[CURLOPT_POSTFIELDS] = $data;
}
if($method == "get" && count($data) > 0) {
$pos = strpos($url, '?');
if ($pos === false) {
$url = $url . '?' . http_build_query($data);
} else {
$url = $url . '&' . http_build_query($data);
}
}
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
$content_size = strlen($content);
$response = array(
"content" => $content,
"size" => $size
);
return $response;
}
}
2018-02-26 06:10:06 +00:00
if(!function_exists("get_web_json")) {
2018-02-26 06:05:25 +00:00
function get_web_json($url, $method="get", $data=array(), $proxy="", $ua="", $ct_out=45, $t_out=45) {
2018-02-26 06:06:47 +00:00
$doc = array();
2018-02-26 06:05:25 +00:00
$raw = get_web_page($url, $method, $data, $proxy, $ua, $ct_out, $t_out);
2018-02-26 06:06:47 +00:00
if($doc['size'] > 0) {
$doc = json_decode($raw);
}
2018-02-26 06:05:25 +00:00
return $doc;
}
}
2018-03-10 16:59:53 +00:00
if(!function_exists("get_web_dom")) {
2018-03-10 17:03:10 +00:00
function get_web_dom($url, $method="get", $data=array(), $proxy="", $ua="", $ct_out=45, $t_out=45) {
2018-03-10 16:59:53 +00:00
$html = new stdClass();
$raw = get_web_page($url, $method, $data, $proxy, $ua, $ct_out, $t_out);
// load simple_html_dom
loadHelper("simple_html_dom");
if(function_exists("str_get_html")) {
$html = str_get_html($raw);
}
2018-03-10 17:00:12 +00:00
2018-03-10 16:59:53 +00:00
return $html;
}
}