科大讯飞公式识别API接口PHP接入
本文记录了借助科大讯飞公式识别 API开放平台,使用PHP构建一个API,实现识别网络图片中的数学公式。一、API构建
创建index.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']['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);
二、请求示例https://你的网络api地址/?pic=待识别的图片的网络地址三、返回数据{
"code": "200",
"src": "https://api.szfx.top/xfitr/demo.png",
"dst": "\\frac {x^ {2}}{a^ {2}}-\\frac {y^ {2}}{b^ {2}}=1(a>0,b>0)"
}四、API应用借助API可以构建的在线公式识别网站比如:https://tool.szfx.top/itr
五、参考资料
1、LaTeX公式识别:https://api.szfx.top/api/xfitr.html
2、科大讯飞公式识别API接口PHP接入:https://www.szfx.top/network-tech/xfyun-itr-latex-recgonition-api.html
3、LaTeX公式识别:https://tool.szfx.top/itr 很高大上,就是不知道后续楼主会不会做一个成品出来呢?
这样对有些学生可能会比较实用。
感谢思路。 牛掰啊,谢谢分享 大神级别,必须点赞 哇,这个厉害,大赞 学习一下,感谢分享 wynewlife 发表于 2024-7-6 01:42
很高大上,就是不知道后续楼主会不会做一个成品出来呢?
这样对有些学生可能会比较实用。
已经有成品了,功能呢还算完整,LaTeX公式识别:https://tool.szfx.top/itr 之前用的是 simpletex,这个也试试看
写论文正需要这个,谢谢!
页:
[1]