[PHP] 纯文本查看 复制代码
<?php
/**
* 公式识别 WebAPI 接口调用示例
* 运行前:请先填写Appid、APIKey、APISecret
*/
class itr_teach_test {
function tocurl($url, $header, $content){
$ch = curl_init();
if(substr($url,0,5)=='https'){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
if (is_array($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
curl_setopt($ch, CURLOPT_POST, true);
if (!empty($content)) {
if (is_array($content)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content));
} else if (is_string($content)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
}
}
$response = curl_exec($ch);
$error=curl_error($ch);
//var_dump($error);
if($error){
die($error);
}
$header = curl_getinfo($ch);
curl_close($ch);
$data = array('header' => $header,'body' => $response);
return $data;
}
function xfyun($pic,$jpg) {
//在控制台-我的应用-公式识别获取
$app_id = "xxxxxx";
//在控制台-我的应用-公式识别获取
$api_sec = "xxxxxx";
//在控制台-我的应用-公式识别获取
$api_key = "xxxxxx";
//图片路径
$resource = $jpg;
$url = "https://rest-api.xfyun.cn/v2/itr";
//body组装
$body = json_encode($this->getBody($app_id, $resource));
// 组装http请求头
//$date = gmstrftime("%a, %d %b %Y %H:%M:%S %Z", time());
$date =gmdate('D, d M Y H:i:s') . ' GMT';
$digestBase64 = "SHA-256=".base64_encode(hash("sha256", $body, true));
$builder = sprintf("host: %s
date: %s
POST /v2/itr HTTP/1.1
digest: %s", "rest-api.xfyun.cn", $date, $digestBase64);
$sha = base64_encode(hash_hmac("sha256", $builder, $api_sec, true));
$authorization = sprintf("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", $api_key, "hmac-sha256", "host date request-line digest", $sha);
$header = [
"Authorization: ".$authorization,
'Content-Type: application/json',
'Accept: application/json,version=1.0',
'Host: rest-api.xfyun.cn',
'Date: '.$date,
'Digest: '.$digestBase64
];
$response = $this->tocurl($url, $header, $body);
//var_dump($response['body']);
$res = json_decode($response['body'], true);
$content = $res['data']['region'][0]['recog']['content'];
$content = str_replace(" ", "", $content);
$content = str_replace("ifly-latex-begin", "", $content);
$content = str_replace("ifly-latex-end", "", $content);
header("content-type: application/json");
if($res['code']=='0'){
$code='200';
}else{
$code='202';
}
$json_return = array(
"code" => $code,
"src" => $pic,
"dst" => ltrim($content)
);
$result = json_encode($json_return, JSON_UNESCAPED_UNICODE);
echo $result;
}
function getBody($app_id, $resource) {
$common_param = [
'app_id' => $app_id
];
$image_data = fread(fopen($resource, 'r'), filesize($resource));
$base64_image = chunk_split(base64_encode($image_data));
$data = [
'image' => $base64_image
];
return $body = [
'common' => $common_param,
'business' => [
'ent' => 'teach-photo-print',
'aue' => 'raw'
],
'data' => $data
];
}
}
/*
*@通过curl方式获取指定的图片到本地
*/
function getImg($url = "", $filename = ""){
$hander = curl_init();
$fp = fopen($filename, 'wb');
curl_setopt($hander, CURLOPT_URL, $url);
curl_setopt($hander, CURLOPT_FILE, $fp);
curl_setopt($hander, CURLOPT_HEADER, 0);
curl_setopt($hander, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($hander, CURLOPT_TIMEOUT, 60);
curl_exec($hander);
curl_close($hander);
fclose($fp);
return true;
}
header('Access-Control-Allow-Origin:*');
header('Content-type: application/json');
$pic=isset($_GET['pic'])? $_GET['pic'] :null;
if(empty($pic)){die("请传入图片参数");}
$time = gmdate('H-i-s', time() + 3600 * 8);
$jpg = $time.'.jpg';
getImg($pic,$jpg);
$a = new itr_teach_test();
$a->xfyun($pic,$jpg);
unlink($jpg);