swlib / saber

⚔️ Saber, PHP异步协程HTTP客户端 | PHP Coroutine HTTP client - Swoole Humanization Library
https://packagist.org/packages/swlib/saber
Apache License 2.0
980 stars 124 forks source link

通过PUT方法 发送文件到服务器 #96

Open hainuo opened 4 years ago

hainuo commented 4 years ago

https://developer.apple.com/documentation/appstoreconnectapi/uploading_assets_to_app_store_connect#3591286

在跟苹果应用商店对接时,需要PUT上传文件,但是目前没有发现何种方式可以不需要key上传一个文件到苹果服务器

twose commented 3 years ago

没懂 是否有HTTP包示例

saber更适合做爬虫而不是上传...

hainuo commented 3 years ago
protected function putFile($url, $path, $contentType)
    {
        $header = [
            "Content-Type: " . $contentType,
            "Content-Length: " . filesize($path)
        ];
        return $this->curlPut($url, $path, $header);
    }
function curlPut($destUrl, $sourceFileDir, $headerArr = [], $timeout = 20)
    {
        $ch = curl_init(); //初始化curl
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回字符串,而不直接输出
        curl_setopt($ch, CURLOPT_URL, $destUrl); //设置put到的url
        curl_setopt($ch, CURLOPT_HEADER, 1); //定义是否显示状态头 1:显示 ; 0:不显示 
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证对等证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //不检查服务器SSL证书

        curl_setopt($ch, CURLOPT_PUT, true); //设置为PUT请求
        curl_setopt($ch, CURLOPT_INFILE, fopen($sourceFileDir, 'rb')); //设置资源句柄
        curl_setopt($ch, CURLOPT_INFILESIZE, filesize($sourceFileDir));

        $response = curl_exec($ch);

        if ($error = curl_error($ch)) {
            $bkArr = [
                'code' => 0,
                'msg' => $error,
            ];
        } else {
            $bkArr = [
                'code' => 1,
                'msg' => 'ok',
                'resp' => $response,
            ];
        }

        curl_close($ch); // 关闭 cURL 释放资源

        return $bkArr;
    }
$returnPutFile = $this->putFile($appScreenshot['data']['attributes']['uploadOperations'][0]['url'], $path, $appScreenshot['data']['attributes']['uploadOperations'][0]['requestHeaders'][0]['value']);

上面是后来用curl自己写的一个, 就是将一个文件直接作为body上传