/** * @return APF_Http_Client_Curl */ public function get_curl() { if (!self::$curl) { self::$curl = $this->load_curl(); } return self::$curl; }
public function getBrokerInfo($brokerid) { $brokerurl = APF::get_instance()->get_config('anjuke_broker_base_url') . '?id=' . $brokerid; $curl = APF_Http_Client_Factory::get_instance()->get_curl(); $curl->set_url($brokerurl); if ($curl->execute()) { $data = $curl->get_response_text(); $data = json_decode($data, true); return $data; } return array(); }
public function get_inline_styles() { $url = $this->get_boundable_styles_url(); $key = "css-" . md5($url); apf_require_class('APF_Cache_Factory'); $mem = APF_Cache_Factory::get_instance()->get_memcache(); $css = $mem->get($key); if ($css) { return $css; } apf_require_class('APF_Http_Client_Factory'); $c = APF_Http_Client_Factory::get_instance()->get_curl(); $c->set_url($url); $c->execute(); $css = $c->get_response_text(); $mem->set($key, $css, 0, 0); return $css; }
public function setMessage($userid, $content) { $content = urlencode($content); $key = md5($userid); $messageApiUrl = APF::get_instance()->get_config('message_api_url') . '?userId=' . $userid . '&content=' . $content . '&key=' . $key; $curl = APF_Http_Client_Factory::get_instance()->get_curl(); $curl->set_url($messageApiUrl); if ($curl->execute()) { $data = $curl->get_response_text(); } }
/** * 获取solr数据 * $solr_url str solr接口地址 * $params array 接口所需参数 数组或字符串 */ private function getSolrData($solr_url = '', $params = array()) { $curl = APF_Http_Client_Factory::get_instance()->get_curl(); if (!empty($params) && is_array($params)) { $solr_url .= http_build_query($params); } elseif (!empty($params) && is_string($params)) { $solr_url .= $params; } $curl->set_url($solr_url); //老代码函数 if ($curl->execute()) { $data = $curl->get_response_text(); return $data; } return false; }
/** * 查询solr(使用apf的curl封装) * @param 字符串 $solr_url solr查询地址 */ public static function get_solr_data2($solr_url) { apf_require_class("APF_Http_Client_Factory"); $curl = APF_Http_Client_Factory::get_instance()->get_curl(); $curl->set_url($solr_url); $r = $curl->execute(); $res = $curl->get_response_text(); $resArray = json_decode($res, true); if ($r) { return $resArray; } else { return array(); } }
/** * 调用API * @param string $p_strURL * @param mix $p_mixData * @param string $p_strMethod * @param string $p_strResultType * @return mix */ private static function _callAPI($p_strURL, $p_mixData, $p_strMethod, $p_strResultType = 'json', $headers = array()) { $p_strData = ''; if (is_array($p_mixData)) { foreach ($p_mixData as $strKey => $strValue) { if (is_array($strValue) || is_object($strValue)) { $strValue = json_encode($strValue); } $p_strData .= '&' . $strKey . '=' . urlencode($strValue); } if (isset($p_strData[0])) { $p_strData = substr($p_strData, 1); } } else { $p_strData = $p_mixData; } apf_require_class('APF_Http_Client_Factory'); $objCURL = APF_Http_Client_Factory::get_instance()->get_curl($p_strMethod); if ($headers) { $entire_headers = array_merge(array("Content-type:application/x-www-form-urlencoded;charset=UTF-8"), $headers); } else { $entire_headers = array("Content-type:application/x-www-form-urlencoded;charset=UTF-8"); } if (!is_array($p_mixData)) { $entire_headers = array_merge(array("Content-type:application/json;charset=UTF-8"), $headers); } $timeout = APF::get_instance()->get_config('back_api_timeout'); $objCURL->set_attribute(CURLOPT_HTTPHEADER, $entire_headers); $objCURL->set_attribute(CURLOPT_CONNECTTIMEOUT, $timeout['connect_timeout']); $objCURL->set_attribute(CURLOPT_TIMEOUT, $timeout['curl_timeout']); $objCURL->set_url($p_strURL); if ('post' == $p_strMethod) { $objCURL->set_url($p_strURL); $objCURL->set_attribute(CURLOPT_POSTFIELDS, $p_strData); if (isset($p_strData[0])) { $p_strURL .= '?' . $p_strData; } } else { if (isset($p_strData[0])) { $p_strURL .= '?' . $p_strData; } $objCURL->set_url($p_strURL); } $strCookies = ''; foreach ($_COOKIE as $strKey => $mixValue) { $strCookies .= ';' . $strKey . '=' . urlencode($mixValue); } $strCookies = substr($strCookies, 1); $objCURL->set_attribute(CURLOPT_COOKIE, $strCookies); $objCURL->set_attribute(CURLOPT_USERAGENT, isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Anjuke API'); if (APF::get_instance()->is_debug_enabled()) { APF::get_instance()->debug('API->Call[' . $p_strMethod . ']: ' . $p_strURL); } $bolResult = $objCURL->execute(); if ($bolResult) { $result['http_code'] = 200; $strResource_json = $objCURL->get_response_text(); switch ($p_strResultType) { case 'json': $strResource = json_decode($strResource_json, true); break; case 'string': break; default: break; } $result['data'] = $strResource; return $result; } else { APF::get_instance()->get_logger()->error('API->Call[' . $p_strMethod . ']: ' . $p_strURL . ' failed.'); APF::get_instance()->get_logger()->error('get_curl_info: ' . print_r($objCURL->get_curl_info(), true)); return false; } }