php请求附件上传请求第三方接口

socket

 

 

/*
 * 附件上传到金融接口
 */
class SocketUpload
{
    private $host = '';
    private $port = 80;
    private $errno = null;
    private $errstr = null;
    public $timeout = 5;
    public $url = '';//请求地址
    private $fp = null;
    private $header = '';  //头信息
    private $body = '';    //消息体
    private $boundary = '----abcdefg'; //指定分隔符号的标志
    private $res = null;  //完整字符串
    private $file = null; //文件
    private $form = null; //表单


    public function __construct($form ='',$file='',$url)
    {
        $this->url = $url;
        $apiConf = config('api');
        $apiConf['urlprefix']['finance'];
        $this->host = substr($apiConf['urlprefix']['finance'],-((strlen($apiConf['urlprefix']['finance'])-(strrpos($apiConf['urlprefix']['finance'],"/")+1))));
        //连接本地80端口
        $this->fp = fsockopen($this->host,$this->port,$this->errno,$this->errstr,$this->timeout);
        if (!$this->fp) exit('failed');
        //赋值
        $this->form = $form;
        $this->file = $file;

        //设置头信息,消息体
        $this->setHead();
        $this->setBody();

        //拼接整个请求信息
        $this->getStr();

    }

    public function response()
    {
        //echo $this->res;
        //写入
        fwrite($this->fp, $this->res);
        //打印输出信息
        $response = '';
        while($row=fread($this->fp, 4096)){
            $response .= $row;
        }
        preg_match("/{.+}/", $response,$b);
        if(!$b || !($b[0])){
            return false;
        }
        $r = json_decode($b[0],1);
        if(!is_array($r)){
            return false;
        }
        return  $r;
        //$pos = strpos($response, "\r\n\r\n");
        //$response = substr($response, $pos+4);

    }

    private function getStr()
    {
        $this->header .= "Content-Length:".strlen($this->body)."\r\n";
        $this->header .= "Connection: close\r\n\r\n";
        $this->res = $this->header.$this->body;
    }

    //设置头信息
    private function setHead()
    {
        $this->header .= "POST {$this->url} HTTP/1.1\r\n";
        $this->header .= "HOST:{$this->host} \r\n";
        $this->header .= "Content-Type:multipart/form-data;  boundary={$this->boundary}\r\n";
    }

    //设置消息体
    private function setBody()
    {
        $this->form();
        $this->file();
    }

    //非文件表单
    private function form()
    {
        if ($this->form && is_array($this->form))
        {
            foreach ($this->form as $key=>$val)
            {
                $this->body .= "--$this->boundary"."\r\n";
                $this->body .= "Content-Disposition: form-data; name=\"{$key}\"\r\n";
                $this->body .= "Content-type:text/plain\r\n\r\n";
                $this->body .= "{$val}\r\n";
            }
        }
    }

    //文件表单
    private function file()
    {
        if ($this->file && is_array($this->file))
        {
            foreach ($this->file as $key=>$val)
            {
                $this->body .= "--$this->boundary"."\r\n";
                $this->body .= "Content-Disposition: form-data; name=\"{$val['name']}\"; filename=\"{$val['filename']}\"\r\n";
                $this->body .= "Content-Type: {$val['type']}\r\n\r\n";
                $this->body .= file_get_contents($val['path'])."\r\n";
                $this->body .= "--{$this->boundary}";
            }

        }
    }

}

 

curl :

 

 public static function fileUpload($url,$fileObj)
    {
        $ch = curl_init();
        //兼容5.0-5.6版本的curl
        if (class_exists('\CURLFile')) {
            $cFile = curl_file_create($fileObj->getRealPath(), $fileObj->getClientMimeType(), $fileObj->getClientOriginalName());
            $data = array('file' => $cFile);
        }else{
            $data = array('file' => '@' . $fileObj->getRealPath());
            if (defined('CURLOPT_SAFE_UPLOAD')) {
                curl_setopt($ch, CURLOPT_SAFE_UPLOAD, FALSE);
            }
        }
        $timeout = 10;

        curl_setopt ( $ch, CURLOPT_URL, $url);
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
        // Execute the handle
        $requestRes = curl_exec($ch);
        curl_close($ch);
        if($requestRes) {
            $requestRes = json_decode($requestRes, true);
            return $requestRes;
        }
    }

你可能感兴趣的:(php开发)