php接入百度AI 文字识别api

1.ceshi.php

<?php
/**
 * 发起http post请求(REST API), 并获取REST请求的结果
* @param string $url
* @param string $param
* @return - http response body if succeeds, else false.
*/
header("Content-Type: text/html; charset=UTF-8");
function request_post($url = '', $param = '')
{
    if (empty($url) || empty($param)) {
        return false;
    }

    $postUrl = $url;
    $curlPost = $param;
    // 初始化curl
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $postUrl);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 要求结果为字符串且输出到屏幕上
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    // post提交方式
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
    // 运行curl
    $data = curl_exec($curl);
    curl_close($curl);

    return $data;
}
//获取access_token
include 'curl.php';
$curl = new Curl();
$access_token = $curl->access_token();
//定义参数并去请求api 
$url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=' . $access_token;
$img = file_get_contents('1.png');
$img = base64_encode($img);
$bodys = array(
        "image" => $img
);
$data = json_decode(request_post($url, $bodys));//解码json数据
//重定义数据
$arr = array();
foreach ($data->words_result as $k=>&$v){
    $arr[]=$v->words;
}
//转换成字符串
var_dump(json_encode($arr));




2.curl.php

<?php 

class Curl{
    public function _request($curl, $https = true,$method='GET',$data = null){
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, $curl);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);//CURLOPT_HEADER 设置头部
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//设置内容
        if($https){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//是否进行服务器主机验证 不验证
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//是否验证证书 验证
            if($method == 'POST'){
                curl_setopt($ch,CURLOPT_POST , true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置数据
            }
            
            $content = curl_exec($ch);//得到一个值
            curl_close($ch);//关闭资源 释放
            return $content;//返回得到的值
        }
    }
    /**
     * 获取API访问授权码
     * @param ak: ak from baidu cloud app
     * @param sk: sk from baidu cloud app
     * @return - access_token string if succeeds, else false.
     */
    public function access_token(){
        //密钥
        $ak = 'HPxjQ2nR0ClpNG9sXWBqGp47';
        $sk = 'n7wsd5Le0hFExb5G2gs0LptPAdSeDUPR';
        //请求地址
        $url = 'https://aip.baidubce.com/oauth/2.0/token';
        
        //参数
        $post_data = array();
        $post_data['grant_type']  = 'client_credentials';
        $post_data['client_id']   = $ak;
        $post_data['client_secret'] = $sk;
        
        //请求方法
        $res = $this->_request($url,true,'POST',$post_data);
        $res = json_decode($res, true);
        return $res['access_token'];
    }
}
?>

    原文作者:jew丶
    原文地址: https://www.jianshu.com/p/e0849f5b9335
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞