1. 程式人生 > 實用技巧 >基於php的Http請求類封裝

基於php的Http請求類封裝

<?php

/**
 *
 * @desc  HTTP 請求類, 支援 CURL 和 Socket, 預設使用 CURL , 當手動指定
 *                useCurl 或者 curl 擴充套件沒有安裝時, 會使用 Socket
 *                目前支援 get 和 post 兩種請求方式
 *
 * 1. 基本 get 請求:
 *    $http = new Http();        // 例項化物件
 *    $result =  $http->get('http://weibo.com/at/comment');
 * 2. 基本 post 請求:
 *    $http = new Http();        // 例項化物件
 *    $result = $http->post('
http://someurl.com/post-new-article', array('title'=>$title, 'body'=>$body) ); * 3. 模擬登入 ( post 和 get 同時使用, 利用 cookie 儲存狀態 ) : * $http = new Http(); // 例項化物件 * $http->setCookiepath(substr(md5($username), 0, 10)); // 設定 cookie, 如果是多個使用者請求的話 * // 提交 post 資料 * $loginData = $http->post('
http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.19)', * array('username'=>$username, 'loginPass'=>$password) ); * $result = $http->get('http://weibo.com/at/comment'); * 4. 利用 initialize 函式設定多個 config 資訊 * $httpConfig['method'] = 'GET'; * $httpConfig['target'] = '
http://www.somedomain.com/index.html'; * $httpConfig['referrer'] = 'http://www.somedomain.com'; * $httpConfig['user_agent'] = 'My Crawler'; * $httpConfig['timeout'] = '30'; * $httpConfig['params'] = array('var1' => 'testvalue', 'var2' => 'somevalue'); * * $http = new Http(); * $http->initialize($httpConfig); * * $result = $http->result; * * 5. 複雜的設定: * * $http = new Http(); * $http->useCurl(false); // 不使用 curl * $http->setMethod('POST'); // 使用 POST method * * // 設定 POST 資料 * $http->addParam('user_name' , 'yourusername'); * $http->addParam('password' , 'yourpassword'); * * // Referrer * $http->setReferrer('https://yourproject.projectpath.com/login'); * * // 開始執行請求 * $http->execute('https://yourproject.projectpath.com/login/authenticate'); * $result = $http->getResult(); * * 6. 獲取開啟了 basic auth 的請求 * * $http = new Http(); * * // Set HTTP basic authentication realms * $http->setAuth('yourusername', 'yourpassword'); * * // 獲取某個被保護的應用的 feed * $http->get('http://www.someblog.com/protected/feed.xml'); * * $result = $http->result; * */ class Http { /** 目標請求 @var string */ var $target; /** 目標 URL 的 host @var string */ var $host; /** 請求目標的埠 @var integer */ var $port; /** 請求目標的 path @var string */ var $path; /** 請求目標的 schema @var string */ var $schema; /** 請求的 method (GET 或者 POST) @var string */ var $method; /** 請求的資料 @var array */ var $params; /** 請求時候的 cookie 資料 @var array */ var $cookies; /** 請求返回的 cookie 資料 @var array */ var $_cookies; /** 請求超時時間, 預設是 25 @var integer */ var $timeout; /** 是否使用 cURL , 預設為 TRUE @var boolean */ var $useCurl; /** referrer 資訊 @var string */ var $referrer; /** 請求客戶端 User agent @var string */ var $userAgent; /** Contains the cookie path (to be used with cURL) @var string */ var $cookiePath; /** 是否使用 Cookie @var boolean */ var $useCookie; /** 是否為下一次請求儲存 Cookie @var boolean */ var $saveCookie; /** HTTP Basic Auth 使用者名稱 (for authentication) @var string */ var $username; /** HTTP Basic Auth 密碼 (for authentication) @var string */ var $password; /** 請求的結果集 @var string */ var $result; /** 最後一個請求的 headers 資訊 @var array */ var $headers; /** Contains the last call's http status code @var string */ var $status; /** 是否跟隨 http redirect 跳轉 @var boolean */ var $redirect; /** 最大 http redirect 調整數 @var integer */ var $maxRedirect; /** 當前請求有多少個 URL @var integer */ var $curRedirect; /** 錯誤程式碼 @var string */ var $error; /** Store the next token @var string */ var $nextToken; /** 是否儲存 bug 資訊 @var boolean */ var $debug; /** Stores the debug messages @var array @todo will keep debug messages */ var $debugMsg; /** Constructor for initializing the class with default values. @return void */ public function __construct() { // 先初始化 $this->clear(); } /** * 初始化配置資訊 * Initialize preferences * * This function will take an associative array of config values and * will initialize the class variables using them. * * Example use: * * <pre> * $httpConfig['method'] = 'GET'; * $httpConfig['target'] = 'http://www.somedomain.com/index.html'; * $httpConfig['referrer'] = 'http://www.somedomain.com'; * $httpConfig['user_agent'] = 'My Crawler'; * $httpConfig['timeout'] = '30'; * $httpConfig['params'] = array('var1' => 'testvalue', 'var2' => 'somevalue'); * * $http = new Http(); * $http->initialize($httpConfig); * </pre> * * @param array Config values as associative array * @return void */ public function initialize($config = array()) { $this->clear(); foreach ($config as $key => $val) { if (isset($this->$key)) { $method = 'set' . ucfirst(str_replace('_', '', $key)); if (method_exists($this, $method)) { $this->$method($val); } else { $this->$key = $val; } } } } /** * 初始化所有 * * Clears all the properties of the class and sets the object to * the beginning state. Very handy if you are doing subsequent calls * with different data. * * @return void */ public function clear() { // Set the request defaults $this->host = ''; $this->port = 0; $this->path = ''; $this->target = ''; $this->method = 'GET'; $this->schema = 'http'; $this->params = array(); $this->headers = array(); $this->cookies = array(); $this->_cookies = array(); // Set the config details $this->debug = FALSE; $this->error = ''; $this->status = 0; $this->timeout = '25'; $this->useCurl = TRUE; $this->referrer = ''; $this->username = ''; $this->password = ''; $this->redirect = TRUE; // Set the cookie and agent defaults $this->nextToken = ''; $this->useCookie = FALSE; $this->saveCookie = FALSE; $this->maxRedirect = 3; $this->cookiePath = 'cookie.txt'; $this->userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7'; } /** 設定目標 @return void */ public function setTarget($url) { $this->target = $url; } /** 設定 http 請求方法 @param string HTTP method to use (GET or POST) @return void */ public function setMethod($method) { $this->method = $method; } /** 設定 referrer URL @param string URL of referrer page @return void */ public function setReferrer($referrer) { $this->referrer = $referrer; } /** 設定 User agent @param string Full user agent string @return void */ public function setUseragent($agent) { $this->userAgent = $agent; } /** 設定請求 timeout @param integer Timeout delay in seconds @return void */ public function setTimeout($seconds) { $this->timeout = $seconds; } /** 設定 cookie path (只支援cURL ) @param string File location of cookiejar @return void */ public function setCookiepath($path) { $this->cookiePath = $path; $this->useCookie(TRUE); $this->saveCookie(TRUE); } /** 設定請求引數 parameters @param array GET or POST 的請求資料 @return void */ public function setParams($dataArray) { $this->params = array_merge($this->params, $dataArray); } /** 設定 basic http auth 域驗證 @param string 使用者名稱 @param string 密碼 @return void */ public function setAuth($username, $password) { $this->username = $username; $this->password = $password; } /** 設定最大跳轉數 @param integer Maximum number of redirects @return void */ public function setMaxredirect($value) { $this->maxRedirect = $value; } /** 新增多一個新的請求資料 @param string Name of the parameter @param string Value of the paramete @return void */ public function addParam($name, $value) { $this->params[$name] = $value; } /** 新增 cookie 請求資料 @param string Name of cookie @param string Value of cookie */ public function addCookie($name, $value) { $this->cookies[$name] = $value; } /** 是否使用 curl, 預設 true, false 為使用 socket */ public function useCurl($value = TRUE) { if (is_bool($value)) { $this->useCurl = $value; } } /** 是否使用 cookie , 預設為 false @param boolean Whether to use cookies or not @return void */ public function useCookie($value = FALSE) { $this->useCookie = $value; } /** 是否使用 cookie , 以供下一次請求使用 @param boolean Whether to save persistent cookies or not @return void */ public function saveCookie($value = FALSE) { $this->saveCookie = $value; } /** 是否跟隨 302 跳轉 @param boolean Whether to follow HTTP redirects or not */ public function followRedirects($value = TRUE) { $this->redirect = $value; } /** 獲取結果集 @return string output of execution */ public function getResult() { return $this->result; } /** 獲取最後一個返回的 headers 陣列 */ public function getHeaders() { return $this->headers; } /** 獲取請求的狀態碼 */ public function getStatus() { return $this->status; } /** 獲取最後執行錯誤 */ public function getError() { return $this->error; } /** 執行一條 http get 請求 */ public function get($url, $data = array()) { $res = $this->execute($url, '', 'get', $data); if (empty($res)) { log_message('error', $url . var_export($data, true)); } return $res; } /** 執行一條 http post 請求 */ public function post($url, $data = array()) { $res = $this->execute($url, '', 'post', $data); if (empty($res)) { log_message('error', $url . var_export($data, true)); } return $res; } /** 執行一條 http upload 請求 */ public function upload($url, $data = array()) { $res = $this->execute($url, '', 'upload', $data); if (empty($res)) { log_message('error', $url . var_export($data, true)); } return $res; } /** * 儲存遠端檔案到本地 * * @param string $request_file_url 遠端的檔案 url * @param string $save_to_filepath 本地儲存的檔案 路徑 * @return void */ public function save($request_file_url, $save_to_filepath) { $fp = fopen($save_to_filepath, 'wb'); return $this->execute($request_file_url, '', 'get', array(), $extra_params = array('CURLOPT_FILE' => $fp)); } /** * 使用當前的配置, 傳送一條 HTTP 請求 * * @param string URL of the target page (optional) * @param string URL of the referrer page (optional) * @param string 請求方法 (GET or POST or upload) (optional) * @param array 請求資料, key 和 value 對應的陣列 (optional) * @return string 請求的結果集 */ public function execute($target = '', $referrer = '', $method = '', $data = array(), $extra_params = array()) { // Populate the properties $this->target = ($target) ? $target : $this->target; $this->method = ($method) ? $method : $this->method; $this->method = strtoupper($this->method); $this->referrer = ($referrer) ? $referrer : $this->referrer; // Add the new params if (is_array($data) && count($data) > 0) { $this->params = array_merge($this->params, $data); } // Process data, if presented if (is_array($this->params) && count($this->params) > 0) { // Get a blank slate $tempString = array(); // Convert data array into a query string (ie animal=dog&sport=baseball) foreach ($this->params as $key => $value) { if (strlen(trim($value)) > 0) { $tempString[] = $key . "=" . urlencode($value); } } $queryString = join('&', $tempString); } // 如果 cURL 沒有安裝就使用 fscokopen 執行請求 $this->useCurl = $this->useCurl && in_array('curl', get_loaded_extensions()); // GET method configuration if ($this->method == 'GET') { if (isset($queryString)) { $this->target = $this->target . "?" . $queryString; } } // Parse target URL $urlParsed = parse_url($this->target); // Handle SSL connection request if ($urlParsed['scheme'] == 'https') { $this->host = 'ssl://' . $urlParsed['host']; $this->port = ($this->port != 0) ? $this->port : 443; } else { $this->host = $urlParsed['host']; $this->port = ($this->port != 0) ? $this->port : 80; } // Finalize the target path $this->path = (isset($urlParsed['path']) ? $urlParsed['path'] : '/') . (isset($urlParsed['query']) ? '?' . $urlParsed['query'] : ''); $this->schema = $urlParsed['scheme']; // Pass the requred cookies $this->_passCookies(); // Process cookies, if requested if (is_array($this->cookies) && count($this->cookies) > 0) { // Get a blank slate $tempString = array(); // Convert cookiesa array into a query string (ie animal=dog&sport=baseball) foreach ($this->cookies as $key => $value) { if (strlen(trim($value)) > 0) { $tempString[] = $key . "=" . urlencode($value); } } $cookieString = join('&', $tempString); } // Do we need to use cURL if ($this->useCurl) { // Initialize PHP cURL handle $ch = curl_init(); // GET method configuration if ($this->method == 'GET') { curl_setopt($ch, CURLOPT_HTTPGET, TRUE); curl_setopt($ch, CURLOPT_POST, FALSE); } else if ($this->method == 'UPLOAD') { //UPLOAD上傳 curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $this->params); curl_setopt($ch, CURLOPT_HTTPGET, FALSE); } else { // POST method configuration if (isset($queryString)) { curl_setopt($ch, CURLOPT_POSTFIELDS, $queryString); } curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_HTTPGET, FALSE); } // Basic Authentication configuration if ($this->username && $this->password) { curl_setopt($ch, CURLOPT_USERPWD, $this->username . ':' . $this->password); } // Custom cookie configuration if ($this->useCookie && isset($cookieString)) { curl_setopt($ch, CURLOPT_COOKIE, $cookieString); } curl_setopt($ch, CURLOPT_HEADER, array('Accept-Language: zh-cn', 'Connection: Keep-Alive', 'Cache-Control: no-cache')); // No need of headers curl_setopt($ch, CURLOPT_NOBODY, FALSE); // Return body curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiePath); // cookie 檔案 curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiePath); // cookie 檔案 // 去掉秒級超時改為毫秒超時 // curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); // Timeout curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->timeout * 1000); // Timeout /** * 繞過libcurl使用standard name server時的bug * http://www.laruence.com/2014/01/21/2939.html */ curl_setopt($ch, CURLOPT_NOSIGNAL, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->timeout * 1000); // 毫秒級超時 curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); // Webbot name curl_setopt($ch, CURLOPT_URL, $this->target); // Target site curl_setopt($ch, CURLOPT_REFERER, $this->referrer); // Referer value curl_setopt($ch, CURLOPT_VERBOSE, FALSE); // Minimize logs curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // No certificate - SSH curl_setopt($ch, CURLOPT_ENCODING, ''); // "Gzip" "deflate" "identity" //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->redirect); // Follow redirects curl_setopt($ch, CURLOPT_MAXREDIRS, $this->maxRedirect); // Limit redirections to four curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // 是否以 string 格式返回 if (isset($extra_params['CURLOPT_FILE'])) { curl_setopt($ch, CURLOPT_FILE, $extra_params['CURLOPT_FILE']); curl_setopt($ch, CURLOPT_HEADER, 0); } //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect')); // Get the target contents $content = curl_exec($ch); // Get the request info $curl_info = curl_getinfo($ch); $header_size = $curl_info["header_size"]; // 值結果集 $this->result = substr($content, $header_size); $this->status = $curl_info['http_code']; // Parse the headers $this->_parseHeaders(explode("\r\n\r\n", trim(substr($content, 0, $header_size)))); // Store the error (is any) $this->_setError(curl_error($ch)); // Close PHP cURL handle curl_close($ch); } else { // Get a file pointer $filePointer = fsockopen($this->host, $this->port, $errorNumber, $errorString, $this->timeout); // We have an error if pointer is not there if (!$filePointer) { $this->_setError('Failed opening http socket connection: ' . $errorString . ' (' . $errorNumber . ')'); return FALSE; } // Set http headers with host, user-agent and content type $requestHeader = $this->method . " " . $this->path . " HTTP/1.1\r\n"; $requestHeader .= "Host: " . $urlParsed['host'] . "\r\n"; $requestHeader .= "User-Agent: " . $this->userAgent . "\r\n"; $requestHeader .= "Content-Type: application/x-www-form-urlencoded\r\n"; // Specify the custom cookies if ($this->useCookie && $cookieString != '') { $requestHeader .= "Cookie: " . $cookieString . "\r\n"; } // POST method configuration if ($this->method == "POST") { $requestHeader .= "Content-Length: " . strlen($queryString) . "\r\n"; } // Specify the referrer if ($this->referrer != '') { $requestHeader .= "Referer: " . $this->referrer . "\r\n"; } // Specify http authentication (basic) if ($this->username && $this->password) { $requestHeader .= "Authorization: Basic " . base64_encode($this->username . ':' . $this->password) . "\r\n"; } $requestHeader .= "Connection: close\r\n\r\n"; // POST method configuration if ($this->method == "POST") { $requestHeader .= $queryString; } // We're ready to launch fwrite($filePointer, $requestHeader); // Clean the slate $responseHeader = ''; $responseContent = ''; // 3...2...1...Launch ! do { $responseHeader .= fread($filePointer, 1); } while (!preg_match('/\\r\\n\\r\\n$/', $responseHeader)); // Parse the headers $this->_parseHeaders($responseHeader); // Do we have a 301/302 redirect ? if (($this->status == '301' || $this->status == '302') && $this->redirect == TRUE) { if ($this->curRedirect < $this->maxRedirect) { // Let's find out the new redirect URL $newUrlParsed = parse_url($this->headers['location']); if ($newUrlParsed['host']) { $newTarget = $this->headers['location']; } else { $newTarget = $this->schema . '://' . $this->host . '/' . $this->headers['location']; } // Reset some of the properties $this->port = 0; $this->status = 0; $this->params = array(); $this->method = 'GET'; $this->referrer = $this->target; // Increase the redirect counter $this->curRedirect++; // Let's go, go, go ! $this->result = $this->execute($newTarget); } else { $this->_setError('Too many redirects.'); return FALSE; } } else { // Nope...so lets get the rest of the contents (non-chunked) if ($this->headers['transfer-encoding'] != 'chunked') { while (!feof($filePointer)) { $responseContent .= fgets($filePointer, 128); } } else { // Get the contents (chunked) while ($chunkLength = hexdec(fgets($filePointer))) { $responseContentChunk = ''; $readLength = 0; while ($readLength < $chunkLength) { $responseContentChunk .= fread($filePointer, $chunkLength - $readLength); $readLength = strlen($responseContentChunk); } $responseContent .= $responseContentChunk; fgets($filePointer); } } // Store the target contents $this->result = chop($responseContent); } } // log data $logarr = array( 'Method' => $this->method, 'Params' => $this->params, 'Result' => $this->result, 'Status' => $this->status, 'Headers' => $this->headers, 'Errors' => $this->error ); // Debug::log($this->target, $logarr ,"HTTP"); // There it is! We have it!! Return to base !!! return $this->result; } /** 解析 header 資訊*/ private function _parseHeaders($responseHeader) { // Break up the headers $headers = $responseHeader; // Clear the header array $this->_clearHeaders(); // Get resposne status if ($this->status == 0) { // Oooops ! if (!preg_match($match = "#^http/[0-9]+\\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)\$#i", $headers[0], $matches)) { $this->_setError('Unexpected HTTP response status'); return FALSE; } // Gotcha! $this->status = $matches[1]; array_shift($headers); } // Prepare all the other headers $_headers = explode('<br />', nl2br($headers[0])); array_shift($_headers); foreach ($_headers as $header) { // Get name and value $headerName = trim(chop(strtolower($this->_tokenize($header, ':')))); // $headerValue = trim(chop($this->_tokenize("\r\n"))); $headerValue = explode(': ', $header)[1]; // If its already there, then add as an array. Otherwise, just keep there if (isset($this->headers[$headerName])) { if (gettype($this->headers[$headerName]) == "string") { $this->headers[$headerName] = array($this->headers[$headerName]); } $this->headers[$headerName][] = $headerValue; } else { $this->headers[$headerName] = $headerValue; } } // Save cookies if asked if ($this->saveCookie && isset($this->headers['set-cookie'])) { $this->_parseCookie(); } } /** 去除所有 header 資訊 */ private function _clearHeaders() { $this->headers = array(); } /** 解析 COOKIE */ private function _parseCookie() { // Get the cookie header as array if (gettype($this->headers['set-cookie']) == "array") { $cookieHeaders = $this->headers['set-cookie']; } else { $cookieHeaders = array($this->headers['set-cookie']); } // Loop through the cookies for ($cookie = 0; $cookie < count($cookieHeaders); $cookie++) { $cookieName = trim($this->_tokenize($cookieHeaders[$cookie], "=")); $cookieValue = $this->_tokenize(";"); $urlParsed = parse_url($this->target); $domain = $urlParsed['host']; $secure = '0'; $path = "/"; $expires = ""; while (($name = trim(urldecode($this->_tokenize("=")))) != "") { $value = urldecode($this->_tokenize(";")); switch ($name) { case "path" : $path = $value; break; case "domain" : $domain = $value; break; case "secure" : $secure = ($value != '') ? '1' : '0'; break; } } $this->_setCookie($cookieName, $cookieValue, $expires, $path, $domain, $secure); } } /** 設定 cookie , 為下一次請求做準備 */ private function _setCookie($name, $value, $expires = "", $path = "/", $domain = "", $secure = 0) { if (strlen($name) == 0) { return ($this->_setError("No valid cookie name was specified.")); } if (strlen($path) == 0 || strcmp($path[0], "/")) { return ($this->_setError("$path is not a valid path for setting cookie $name.")); } if ($domain == "" || !strpos($domain, ".", $domain[0] == "." ? 1 : 0)) { return ($this->_setError("$domain is not a valid domain for setting cookie $name.")); } $domain = strtolower($domain); if (!strcmp($domain[0], ".")) { $domain = substr($domain, 1); } $name = $this->_encodeCookie($name, true); $value = $this->_encodeCookie($value, false); $secure = intval($secure); $this->_cookies[] = array("name" => $name, "value" => $value, "domain" => $domain, "path" => $path, "expires" => $expires, "secure" => $secure ); } /** cookie 資料集編碼 */ private function _encodeCookie($value, $name) { return ($name ? str_replace("=", "%25", $value) : str_replace(";", "%3B", $value)); } /** 把正確的 cookie 傳輸給當前請求 */ private function _passCookies() { if (is_array($this->_cookies) && count($this->_cookies) > 0) { $urlParsed = parse_url($this->target); $tempCookies = array(); foreach ($this->_cookies as $cookie) { if ($this->_domainMatch($urlParsed['host'], $cookie['domain']) && (0 === strpos($urlParsed['path'], $cookie['path'])) && (empty($cookie['secure']) || $urlParsed['protocol'] == 'https') ) { $tempCookies[$cookie['name']][strlen($cookie['path'])] = $cookie['value']; } } // cookies with longer paths go first foreach ($tempCookies as $name => $values) { krsort($values); foreach ($values as $value) { $this->addCookie($name, $value); } } } } /** 匹配域名 */ private function _domainMatch($requestHost, $cookieDomain) { if ('.' != $cookieDomain{0}) { return $requestHost == $cookieDomain; } elseif (substr_count($cookieDomain, '.') < 2) { return false; } else { return substr('.' . $requestHost, -strlen($cookieDomain)) == $cookieDomain; } } /** 給當前操作做記號用的 */ private function _tokenize($string, $separator = '') { if (!strcmp($separator, '')) { $separator = $string; $string = $this->nextToken; } for ($character = 0; $character < strlen($separator); $character++) { if (gettype($position = strpos($string, $separator[$character])) == "integer") { $found = (isset($found) ? min($found, $position) : $position); } } if (isset($found)) { $this->nextToken = substr($string, $found + 1); return (substr($string, 0, $found)); } else { $this->nextToken = ''; return ($string); } } /** 設定錯誤資訊 */ private function _setError($error) { if ($error != '') { $this->error = $error; return $error; } } } ?>
$response = $http_service->$method($url, $param);
        if (empty($response) || !$response = @json_decode($response, true)) {
            log_message('error', "empty response for {$url} , params " . json_encode($param));
            return false;
        } else if ($response['status'] != 200) {
            log_message('error', "invalid response for {$url} , params " . json_encode($param) . ", response:" . $response['msg']);
            return false;
        } else {
            return $response['msg'];
        }