PHP图片内容以二进制数据流的形式发送(CurlFile使用)

//第一个参数:图片路径(包括图片文件)
//第二个参数:图片格式
//第三个参数:图片名称
$obj_cashCard = new CurlFile($param['img_path'], "image/jpg", $param['file_name'] . '.jpg');

实例: 

PHP图片内容以二进制数据流的形式发送(CurlFile使用)_第1张图片

/**
     * 日志记录
     * @param $data /内容
     * @param $method /方法名
     * @param $phone /手机号
     * @param string $is_big /通道别名
     * @param string $title /标题
     */
    public function Log($data, $method, $is_big, $title, $phone = '')
    {
        $str = is_array($data) ? json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) : $data;
        if (empty($phone)) {
            $content = '【' . date('Y-m-d H:i:s') . '】' . $title . $str . PHP_EOL;
        } else {
            $content = ' phone:' . $phone . '【' . date('Y-m-d H:i:s') . '】' . $title . $str . PHP_EOL;
        }
        $path = APPPATH . "../PAYLOG/$is_big/$method/";

        if (!is_dir($path)) { //判断目录是否存在 不存在就创建
            mkdir($path, 0777, true);
        }
        file_put_contents($path . date("Y-m-d") . '.txt', $content, FILE_APPEND);
        //换行分割
        file_put_contents($path . date("Y-m-d") . '.txt', '------------------------' . PHP_EOL, FILE_APPEND);
    }

    // 上传图片post
    public function imgUploadPost($url, $data = array())
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_URL, $url);//上传类
        curl_setopt($ch, CURLOPT_TIMEOUT, 40);
        $result = curl_exec($ch);
        if (0 != curl_errno($ch)) {
            $result['error'] = "Error:\n" . curl_error($ch);

        }
        $httpCodes = curl_getinfo($ch);
        curl_close($ch);
        return $result;
    }

    // 上传图片签名+post
    public function imgsign_post($param, $method, $api_method, $is_big, $phone)
    {
        $input['reqTime'] = date("YmdHis");
        //渠道号
        $input['partnerId'] = $this->partnerId;
        $this->log($input, $api_method, $is_big, '签名前数据:', $phone);

        //签名
        $input['signature'] = $this->makeSign($input);
        $this->log($input['signature'], $api_method, $is_big, '签名值:');

        //图片
        $obj_cashCard = new CurlFile($param['img_path'], "image/jpg", $param['file_name'] . '.jpg');
        $input['picture'] = $obj_cashCard;
        $post_url = $this->zdUrl . $method;
        ksort($input);
        $this->log($post_url, $api_method, $is_big, '请求url:');
        $this->log($input, $api_method, $is_big, '请求报文:');

        $rs = $this->imgUploadPost($post_url, $input);
        $this->log($rs, $api_method, $is_big, '响应报文:');

        return $rs;
    }

 

 

你可能感兴趣的:(PHP)