如何在PHP中编写curl -X PUT -T脚本

时间:2018-09-13 05:48:28

标签: php curl php-curl

我正在研究需要使用cURL PUT请求上传文件的Hootsuite脚本。在Hootsuite网站上,我找到了以下示例代码,但可以在PHP脚本中转换以下代码:

curl -X PUT -H 'Content-Type: video/mp4' \
         -H "Content-Length: 8036821" \
         -T "/Users/kenh/Downloads/amazing_race.mp4" \
         "https://hootsuite-video.s3.amazonaws.com/production/3563111_6923ef29-d2bd-4b2a-a6d2-11295411c988.mp4?AWSAccessKeyId=AKIAIHSDDN2I7V2FDJGA&Expires=1465846288&Signature=3xLFijSn02YIx6AOOjmri5Djkko%3D"

我尝试了以下代码,但仍无法正常工作:

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://hootsuite-video.s3.amazonaws.com/production/3563111_6923ef29-d2bd-4b2a-a6d2-11295411c988.mp4?AWSAccessKeyId=AKIAIHSDDN2I7V2FDJGA&Expires=1465846288&Signature=3xLFijSn02YIx6AOOjmri5Djkko%3D",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "PUT",
            CURLOPT_POSTFIELDS => array('file' => '@' . realpath('/Users/kenh/Downloads/amazing_race.mp4')),
            CURLOPT_HTTPHEADER => array(
              'Content-Type: video/mp4',
              'Content-Length: 8036821'    
            )
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

因此,有人可以告诉我如何在PHP中转换上述代码吗? 我会很感激的。

1 个答案:

答案 0 :(得分:0)

尝试在http_build_query选项上附加CURLOPT_POSTFIELDS,如下所示:

CURLOPT_POSTFIELDS => http_build_query( array( 'file' => '@' . realpath( '/Users/kenh/Downloads/amazing_race.mp4' ) ) ),

据我所知,在使用http_build_query方法时,在帖子字段中必须使用PUT