[英]Using PHP curl to run a PUT command on a zip file on PHP 5.3.8
您好,我需要使用 curl 在 PHP 中执行此 PUT 命令,但我在运行时遇到了问题。 需要传输的文件是zip文件。 这是 curl 命令:
curl -X PUT -H "Content-Type:application/zip" --data-binary @yZip.zip http://183.262.144.266:1211/y-validation/repository/HELLO
这是我到目前为止的代码
$ch = curl_init();
$filepath = 'yZip.zip';
curl_setopt($ch, CURLOPT_URL, 'http://183.262.144.266:1211/y-validation/repository/HELLO');
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
$fh_res = fopen($filePath, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath));
curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
$curl_response = curl_exec ($ch);
print_r($curl_response);
我从各种网站上获取了这段代码,但我不断收到错误消息,不知道下一步该怎么做?有什么想法吗?
更新:修复了错误,我成功链接了 REST API 但 zip 文件未正确上传。
更新 2:更新了我为尝试解决问题所做的代码更改,但 zip 仍未正确放置。 另外我正在研究 PHP 5.3.8,所以不能使用 CurlFile class。有人可以帮忙吗?
更新 3:仍然有问题,试图实现标头但那也不起作用任何人都可以帮助我吗?
我尝试这段代码并为我工作......
<?php
set_time_limit(600);
$ch = curl_init();
$filePath = 'file.zip';
curl_setopt($ch, CURLOPT_URL, 'http://site/reciver_script_name/uploading/path/name');
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_UPLOAD, 1);
$fh_res = fopen($filePath, 'r');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/zip'));
curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath));
curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
$curl_response = curl_exec ($ch);
print_r($curl_response);
?>
和接收方:
一些路径提示...
$path = $_SERVER['SCRIPT_NAME'];
if (substr($path, 0, strlen($prefix)) != $prefix) {
exit_not_found();
}
$path = substr($path, strlen($prefix));
$parts = explode('/', $path);
if (!is_array($parts) || count($parts) != 3) {
exit_not_found();
}
和主要部分...
if (!$error) {
$file_stream = fopen($file, 'w');
if ($file_stream === false) {
$error = 'fopen failed';
}
}
if (!$error) {
$copy_result = stream_copy_to_stream(fopen('php://input', 'r'), $file_stream);
fclose($file_stream);
if (!$copy_result) {
$error = 'stream_copy_to_stream failed';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.