php fsockopen例子

<?php

/*
* 短信相关api
*/

class Webcall {

protected $VCC_CODE; //api的账号
protected $password; //api的密码
protected $api_url; //api的地址

/*
* 设置api相关参数
* */

public function __construct() {
$this->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 ";
$httpheader .= "Host:" . $url_info['host'] . " ";
$httpheader .= "Content-Type:application/x-www-form-urlencoded ";
$httpheader .= "Content-Length:" . strlen(http_build_query($param)) . " ";
$httpheader .=$this->get_auth();
$httpheader .= "Connection:close ";
$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 . "" ";
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;
}

}

原文地址:https://www.cnblogs.com/lijiageng/p/6029952.html