最近想学习一下旷世的 face++人脸识别,参照文档用 php 写了个脚本,传本地的照片上去测试时发现老是不成功,研究了好久才发现 curl 函数进行 post 请求不成功,试了好多办法也没法正常使用。。。
<?php
function curl($image){
$fp = fopen(dirname(__FILE__).'/errorlog.txt', 'a');
$ch = curl_init();
$post_data = array(
'api_key'=>'key',
'api_secret'=>'secret',
'image_file' => "$image"
);
print_r($post_data);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $fp);
//curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_URL, "https://api-cn.faceplusplus.com/facepp/v3/detect");
$info= curl_exec($ch);
curl_close($ch);
print_r($info);
return $info;
}
$image='@image.jpg';
$re=curl($image);
print_r($re);
ini_set("max_execution_time", 300);
附 curl 的日志如下
Host: api-cn.faceplusplus.com
User-Agent: Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)
Accept: */*
Accept-Encoding: gzip, deflate
Content-Length: 19061
Content-Type: multipart/form-data; boundary=------------------------a3ed34acf8fa0825
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
< HTTP/2 400
< server: Tengine
< date: Tue, 11 Feb 2020 16:24:57 GMT
< content-type: application/json; charset=utf-8
< content-length: 157
* HTTP error before end of send, stop sending
<
* Connection #0 to host api-cn.faceplusplus.com left intact
1
stabc 2020-02-12 00:47:54 +08:00
这种类型的应用还是别用 PHP 了吧
|
2
zhangxu128 OP 已解决,使用了 curl_file_create 函数来创建文件上传对象。
|
3
zhangxu128 OP @stabc 有在考虑 用 php 只是因为用着顺手 23333
|