首先他们数据请求类型有两种一种Url和Image 具体看你自己是要用哪一种了,
我这边采用了Image的方式,
然后我很菜所以还是要看文档的毕竟是用人家的东西嘛,看文档讲道理是没问题的,
好了废话不多说开始吧。请忽略代码规范.....毕竟我还是个菜鸡 谢谢。
先生成一波签名,至于为什么要签名不要问我谢谢。。。
<?php
$appid= $Config['appid'];
$secretId= $Config['secretId'];
$secretKey= $Config['secretKey'];
$bucket = $Config['bucket'];
$expired= time() + 2592000;
$onceExpired= 0;
$current = time();
$rdm= rand();
$userid= "0";
$fileid = "";
$srcStr='a='.$appid.'&b='.$bucket.'&k='.$secretId.'&e='.$expired.'&t='.$current.'&r='.$rdm.'&u='.$userid.'&f='; $signStr = base64_encode(hash_hmac('SHA1', $srcStr, $secretKey, true).$srcStr); $images = __DIR__ . '/1.png'; //要上传的图片
接下来-----接着上面的写 ps照着官方给的文档写的
<?php
$appid = $Config['appid'];
$secretId = $Config['secretId'];
$secretKey = $Config['secretKey'];
$bucket = $Config['bucket'];
$expired = time() + 2592000;
$onceExpired= 0;
$current = time();
$rdm = rand();
$userid = "0";
$fileid = "";
$srcStr='a='.$appid.'&b='.$bucket.'&k='.$secretId.'&e='.$expired.'&t='.$current.'&r='.$rdm.'&u='.$userid.'&f='; $signStr = base64_encode(hash_hmac('SHA1', $srcStr, $secretKey, true).$srcStr);
$images = __DIR__ . '/1.png'; //要上传的图片 我本地的。。。
function tocurl($content){
global $signStr, $images;
$header = []; //储存Header头信息
$header[] = 'Host: recognition.image.myqcloud.com';
$header[] = 'Authorization: '.$signStr;
$header[] = 'Content-Type: multipart/form-data';
$header[] = 'Content-Length:'.filesize($content);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://recognition.image.myqcloud.com/ocr/handwriting');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
$response = curl_exec($ch); curl_close($ch);
return $response;
}
$res = tocurl(['appid' => $appid,'bucket'=>$bucket ,'image' => '@'. $images]);
var_dump($res );
so 到这里写完了 是时候运行一波了
然而。。。给我来了个报错。。。。
木有错按照他们的文档一样一样的来 报错了 我真的是万马奔腾。。。。
然而怎么解决呢 我抱着随手一试的心态把代码小小的一改 。。
<?php
$appid = $Config['appid'];
$secretId = $Config['secretId'];
$secretKey = $Config['secretKey'];
$bucket = $Config['bucket'];
$expired = time() + 2592000;
$onceExpired= 0;
$current = time();
$rdm = rand();
$userid = "0";
$fileid = "";
$srcStr='a='.$appid.'&b='.$bucket.'&k='.$secretId.'&e='.$expired.'&t='.$current.'&r='.$rdm.'&u='.$userid.'&f='; $signStr = base64_encode(hash_hmac('SHA1', $srcStr, $secretKey, true).$srcStr);
$images = __DIR__ . '/1.png'; //要上传的图片 我本地的。。。
function tocurl($content){
global $signStr, $images;
$header = []; //储存Header头信息
$header[] = 'Host: recognition.image.myqcloud.com';
$header[] = 'Authorization: '.$signStr;
$header[] = 'Content-Type: multipart/form-data';
//$header[] = 'Content-Length:'.filesize($content);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://recognition.image.myqcloud.com/ocr/handwriting');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
$response = curl_exec($ch);
curl_close($ch); return $response;
}
$res = tocurl(['appid' => $appid,'bucket'=>$bucket ,'image' => '@'. $images]); var_dump($res );
然后运行一下。。
奇迹般的成功了 没看错 我只是把官方文档中要我们写的Content-Length去掉了就好了
对于为什么他们官方为啥要我写Length却报错我也不是很懂,可能是我菜吧。
但是官方文档能不能对我这个拍黄片额PHP友好一点 一下午就因为这个东西浪费了 真的是心态爆炸好吧 所以在他们还没改回来之前写了一个这个,我也不知道这是bug还是我请求方式的问题 反正就是按照文档写我特么错了。。。。如果是bug希望他们早些更正吧 第一次写,希望有点用吧