PHP 的 curl 发 post 请求时老是中断,大伙帮忙看看问题出哪儿了

2020-02-12 00:28:41 +08:00
 zhangxu128

最近想学习一下旷世的 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
2451 次点击
所在节点    PHP
3 条回复
stabc
2020-02-12 00:47:54 +08:00
这种类型的应用还是别用 PHP 了吧
zhangxu128
2020-02-12 01:13:50 +08:00
已解决,使用了 curl_file_create 函数来创建文件上传对象。
zhangxu128
2020-02-12 01:14:14 +08:00
@stabc 有在考虑 用 php 只是因为用着顺手 23333

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/643904

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX