百度AI的OCR通用文字识别调用改成链接访问

遇到个奇葩问题,百度ocr的sdk说我没权限……{"error_msg":"No permission to access data","error_code":6}

问题是我肯定是有权限的,只好换成链接直接访问的形式


$fileurl = $img; // 图片的链接
$posturl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate?access_token=';

$url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=你的APIKey&client_secret=你的SecretKey';
$urlcontent = file_get_contents ( $url );
$urlcontent = json_decode ( $urlcontent, true );
$access_token = $urlcontent ['access_token'];// 申请一个access_token
echo $access_token . BR;
$url = $posturl . $access_token;
$img = file_get_contents ( $fileurl );
$img = base64_encode ( $img );//把图片转成base64扔过去

$bodys = array ("image" => $img );
$res = request_post ( $url, $bodys );
$res = json_decode ( $res, true );
print_r ( $res );
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_HTTPHEADER, array ('Content-Type:application/x-www-form-urlencoded' ) );
    // 要求结果为字符串且输出到屏幕上
    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;
}
die;
发布了14 篇原创文章 · 获赞 3 · 访问量 4403

猜你喜欢

转载自blog.csdn.net/kina100/article/details/86496847