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;
        }
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

攻城狮的梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值