如何在PHP中使用fic curl代码

时间:2016-11-11 07:48:27

标签: php curl

如何在curl php下面的代码修复

curl -X POST https://content.dropboxapi.com/2/files/upload \
  --header 'Authorization: Bearer Cf6jn41fHMAAAAAAAAAAgpH1o7IHHaUTWZNNdFSYVlcLb8K-fcnLXbEXnd3lK6Mv' \
  --header 'Content-Type: application/octet-stream' \
  --header 'Dropbox-API-Arg: {"path":"/my.html","mode":{".tag":"add"},"autorename":true}' \
  --data-binary @'upload.php'

1 个答案:

答案 0 :(得分:3)

您正在使用CURL 通过POST执行二进制文件上传,请尝试以下方法:

$YourString = file_get_contents("upload.php");
$stream = fopen('php://memory','r+');
fwrite($stream, $YourString );
$dataLength = ftell($stream);
rewind($stream);

$curl = curl_init();
curl_setopt_array( $curl,
    array( CURLOPT_CUSTOMREQUEST => 'POST'
    , CURLOPT_URL => 'https://content.dropboxapi.com/2/files/upload'
    , CURLOPT_HTTPHEADER => array('Authorization: Bearer Cf6jn41fHMAAAAAAAAAAgpH1o7IHHaUTWZNNdFSYVlcLb8K-fcnLXbEXnd3lK6Mv','Content-Type: application/octet-stream','Dropbox-API-Arg: {"path":"/my.html","mode":{".tag":"add"},"autorename":true}')
    , CURLOPT_RETURNTRANSFER => 1                     // means output will be a return value from curl_exec() instead of simply echoed
    , CURLOPT_TIMEOUT => 15                           // max seconds to wait
    , CURLOPT_FOLLOWLOCATION => 0                     // don't follow any Location headers, use only the CURLOPT_URL, this is for security
    , CURLOPT_FAILONERROR => 0                        // do not fail verbosely fi the http_code is an error, this is for security
    , CURLOPT_SSL_VERIFYPEER => 1                     // do verify the SSL of CURLOPT_URL, this is for security
    , CURLOPT_VERBOSE => 0                            // don't output verbosely to stderr, this is for security
    , CURLOPT_INFILE => $stream
    , CURLOPT_INFILESIZE => $dataLength
    , CURLOPT_UPLOAD => 1
    ) );

$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
echo($response.'<br/>');
echo($http_code.'<br/>');