V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zhangxu128
V2EX  ›  PHP

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

  •  
  •   zhangxu128 · 2020-02-12 00:28:41 +08:00 · 2430 次点击
    这是一个创建于 1507 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近想学习一下旷世的 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
    
    stabc
        1
    stabc  
       2020-02-12 00:47:54 +08:00
    这种类型的应用还是别用 PHP 了吧
    zhangxu128
        2
    zhangxu128  
    OP
       2020-02-12 01:13:50 +08:00
    已解决,使用了 curl_file_create 函数来创建文件上传对象。
    zhangxu128
        3
    zhangxu128  
    OP
       2020-02-12 01:14:14 +08:00
    @stabc 有在考虑 用 php 只是因为用着顺手 23333
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2808 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 447ms · UTC 11:47 · PVG 19:47 · LAX 04:47 · JFK 07:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.