php fsockopen例子

Original 2016-11-04 14:10:28 332
abstract:
CI = & get_instance(); $this->CI->config->load('config.php'); $sms_config = $this->CI->config->item('callcenter'); $this->api_url = $sms_config['api_url']; $this->VCC_CODE = $sms_config['vcc_code']; $this->secret = $sms_config['secret']; } /* * 请求打电话接口 * @param array $param 要发送的参数 * */ public function post_api($param = array()) { $url_info = parse_url($this->api_url); $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n"; $httpheader .= "Host:" . $url_info['host'] . "\r\n"; $httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n"; $httpheader .= "Content-Length:" . strlen(http_build_query($param)) . "\r\n"; $httpheader .=$this->get_auth(); $httpheader .= "Connection:close\r\n\r\n"; $httpheader .= http_build_query($param); $fd = fsockopen($url_info['host'], 80); fwrite($fd, $httpheader); $gets = ""; while(!feof($fd)) { $gets .= fread($fd, 128); } fclose($fd); return $gets; } /** * 生成调用电话接口的验证数据 * @param none * */ public function get_auth() { $nonce = rand(100000, 999999); //base64_encode('žY');//$this->CI->security->get_random_bytes(5)); $time = time(); $PasswordDigest = base64_encode(sha1(base64_decode($nonce) . $time . $this->secret, true)); $header = "X-WSSE:UsernameToken Username=\"" . $this->VCC_CODE . "\",PasswordDigest=\"" . $PasswordDigest . "\",Nonce=\"" . $nonce . "\",Created=\"" . $time . "\"\r\n"; return $header; } /** * 医生打电话给用户 * @param string $caller 先接通一方号码 * @param string $called 后接通一方号码 * */ public function call($caller, $called) { $params = array('vcc_code' => $this->VCC_CODE, 'caller' => $caller, 'called' => $called, 'display_caller' => '59222733', 'display_called' => '59222733'); $res = $this->post_api($params); if (strpos($res, 'ok') !== FALSE) { return TRUE; } return FALSE; } }


Release Notes

Popular Entries