如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
<?php /** * 微软小冰颜值测试 */ class IceFace { // 颜值测试首页 protected $page = 'http://kan.msxiaobing.com/ImageGame/Portal?task=yanzhi'; // 颜值测试接口 protected $api = 'http://kan.msxiaobing.com/Api/ImageAnalyze/Process?service=yanzhi'; // 上传图片接口 protected $fileApi = 'http://kan.msxiaobing.com/Api/Image/UploadBase64'; // 错误信息 protected $error = ''; // 保存会话的文件 protected $cookieFile = ''; // 原数据 public $srcData = []; // 评分 public $score = 0; // 调侃 public $ridicule = ''; // 人脸图片 public $imgOfFace = ''; // 原图片文件 public $srcImgPath = ''; /** * 构造函数 * @param string $file_path 本地或网络的文件路径 */ public function __construct($file_path) { $this->srcImgPath = $file_path; // 建立一个零时文件 $this->cookieFile = tempnam('./temp', 'cookie'); } /** * 颜值测试 * @return boolean 测试是否成功 */ public function test() { $ret = $this->send($this->page); if (empty($ret)) { return $this->setError('访问颜值测试主页失败'); } $ret = $this->uploadImgage($this->srcImgPath); if (!isset($ret->Host) || !isset($ret->Host)) { return $this->setError('上传图片到微软服务器错误'); } $data = [ 'MsgId' => str_pad(time(), 13, '0'), 'CreateTime' => time(), 'Content[imageUrl]' => $ret->Host . $ret->Url, ]; $headers = [ 'Referer' => $this->page, 'User-Agent' => 'Python-urllib/2.7', 'Connection' => 'close', ]; $rsp = $this->send($this->api, $data, 'POST', $headers); if (empty($rsp)) { return $this->setError('颜值测试失败'); } $this->srcData = json_decode($rsp, true); if (!isset($this->srcData['content'])) { return $this->setError('返回的数据异常'); } // 解析原始数据 $this->paramSrcData(); } /** * 获取错误信息 * @return string */ public function getError() { return $this->error; } /** * 解析原始数据 */ private function paramSrcData() { if (empty($this->srcData)) { return; } preg_match('/[-+]?([0-9]*\.[0-9]+|[0-9]+)/', $this->srcData['content']['text'], $matches); if (!empty($matches) && isset($matches[0])) { $this->score = $matches[0]; } $this->ridicule = $this->srcData['content']['text']; $this->imgOfFace = $this->srcData['content']['imageUrl']; } /** * 设置错误信息 * @return boolean */ private function setError($message = '') { $this->error = $message; return false; } /** * 上传图片 * @param string $file_path 本地或网络地址 * @return data|string 响应数据 */ private function uploadImgage($file_path) { $img_data = $this->getBase64DataByFilePath($file_path); $rsp = $this->send($this->fileApi, $img_data, 'post', [], true); if (empty($rsp)) { return fasle; } return json_decode($rsp); } /** * 发出请求 * @param string $url URL * @param array|string $request 请求参数 * @param string $method 请求方法 * @return string */ private function send($url, $request = null, $method = 'GET', $headers = [], $is_binary = false) { $ch = curl_init(); $method = strtoupper($method); if ($method == 'GET') { if (is_array($request)) { $url = $url . '?' . http_build_query($request); } } if ($method == 'POST') { curl_setopt($ch, CURLOPT_POST, true); if ($is_binary) { curl_setopt($ch, CURLOPT_POSTFIELDS, $request); } else { if (is_array($request)) { curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request)); } } } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $this->paramHeader($headers)); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // cookie文件 curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieFile); curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookieFile); $res = curl_exec($ch); curl_close($ch); return $res; } /** * 解析header * @param array $headers * @return array 解析后的头部数组 */ private function paramHeader($headers) { $arr = []; foreach ($headers as $key => $value) { $arr[] = $key . ':' . $value; } return $arr; } /** * 获取图片的Base64编码的数据 * @param string $file_path 本地或网络地址 * @return string|base64 */ private function getBase64DataByFilePath($file_path) { return base64_encode(file_get_contents($file_path)); } } $o = new IceFace('https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1527267326820&di=ed1e3eac833f7cee00cce44aca19cbc5&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F7c1ed21b0ef41bd5221ae1d15bda81cb39db3d4d.jpg'); $o->test(); echo "<pre>"; // print_r($o->score); // print_r($o->ridicule); // print_r($o->imgOfFace); print_r($o);