图片上传 CURL 命令到 PHP Curl
Image upload CURL command to PHP Curl
我是 PHP CURL 的新手,正试图调用 API 来上传图片(一次上传多个文件)。 API 文档在 CURL 中给出了以下示例。我已经测试过,它正在工作。
curl -H "Authorization: Bearer 123456789" -i -X POST -F "whitespace=1" \
-F "imageData[]=@/path/to/images/milk.jpg" \
-F "imageData[]=@/path/to/images/peas.jpg" \
https://mytestapi.com/1.0/uploadimages
现在我需要将其转换为 PHP Curl。出于某种原因,我总是收到错误 "imageData" param invalid。有人可以帮忙吗
$token = '123456789';
$imgUrl = 'https://mytestapi.com/1.0/uploadimages';
$data_to_post = array();
$data_to_post['whitespace'] = '1';
$data_to_post['imageData[]'] = '@/path/to/images/milk.jpg';
$data_to_post['imageData[]'] = '@/path/to/images/peas.jpg';
$curl = curl_init($imgUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer '.$token]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl,CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS, $data_to_post);
$data = json_decode(curl_exec($curl));
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
var_dump($data);
尝试给出图片的完整路径而不是'@/path/to/images/milk.jpg';
这是更新后的代码:
$token = '123456789';
$imgUrl = 'https://mytestapi.com/1.0/uploadimages';
$data_to_post = array();
$data_to_post['whitespace'] = '1';
$data_to_post['imageData'][] = "@".$imgUrl.'/path/to/images/milk.jpg';
$data_to_post['imageData'][] = "@".$imgUrl.'/path/to/images/peas.jpg';
$curl = curl_init($imgUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer '.$token]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl,CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS, $data_to_post);
$data = json_decode(curl_exec($curl));
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
var_dump($data);
如果这不起作用,请告诉我。
我自己解决了这个问题。必须使用 php curl file create 来使其工作而不是附加 '@'
如何使用 PHP cURL
上传文件
<?php
// Helper function courtesy of https://github.com/guzzle/guzzle/blob/3a0787217e6c0246b457e637ddd33332efea1d2a/src/Guzzle/Http/Message/PostFile.php#L90
function getCurlValue($filename, $contentType, $postname)
{
// PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
// See: https://wiki.php.net/rfc/curl-file-upload
if (function_exists('curl_file_create')) {
return curl_file_create($filename, $contentType, $postname);
}
// Use the old style if using an older version of PHP
$value = "@{$this->filename};filename=" . $postname;
if ($contentType) {
$value .= ';type=' . $contentType;
}
return $value;
}
$filename = '/path/to/file.jpg';
$cfile = getCurlValue($filename,'image/jpeg','cattle-01.jpg');
//NOTE: The top level key in the array is important, as some apis will insist that it is 'file'.
$data = array('file' => $cfile);
$ch = curl_init();
$options = array(CURLOPT_URL => 'http://your/server/api/upload',
CURLOPT_RETURNTRANSFER => true,
CURLINFO_HEADER_OUT => true, //Request header
CURLOPT_HEADER => true, //Return header
CURLOPT_SSL_VERIFYPEER => false, //Don't veryify server certificate
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
$header_info = curl_getinfo($ch,CURLINFO_HEADER_OUT);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = substr($result, $header_size);
curl_close($ch);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>File Upload results</title>
</head>
<body>
<p>Raw Result: <?=$result?>
<p>Header Sent: <?=$header_info?></p>
<p>Header Received: <?=$header?></p>
<p>Body: <?=$body?></p>
</body>
</html>
我是 PHP CURL 的新手,正试图调用 API 来上传图片(一次上传多个文件)。 API 文档在 CURL 中给出了以下示例。我已经测试过,它正在工作。
curl -H "Authorization: Bearer 123456789" -i -X POST -F "whitespace=1" \
-F "imageData[]=@/path/to/images/milk.jpg" \
-F "imageData[]=@/path/to/images/peas.jpg" \
https://mytestapi.com/1.0/uploadimages
现在我需要将其转换为 PHP Curl。出于某种原因,我总是收到错误 "imageData" param invalid。有人可以帮忙吗
$token = '123456789';
$imgUrl = 'https://mytestapi.com/1.0/uploadimages';
$data_to_post = array();
$data_to_post['whitespace'] = '1';
$data_to_post['imageData[]'] = '@/path/to/images/milk.jpg';
$data_to_post['imageData[]'] = '@/path/to/images/peas.jpg';
$curl = curl_init($imgUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer '.$token]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl,CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS, $data_to_post);
$data = json_decode(curl_exec($curl));
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
var_dump($data);
尝试给出图片的完整路径而不是'@/path/to/images/milk.jpg';
这是更新后的代码:
$token = '123456789';
$imgUrl = 'https://mytestapi.com/1.0/uploadimages';
$data_to_post = array();
$data_to_post['whitespace'] = '1';
$data_to_post['imageData'][] = "@".$imgUrl.'/path/to/images/milk.jpg';
$data_to_post['imageData'][] = "@".$imgUrl.'/path/to/images/peas.jpg';
$curl = curl_init($imgUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer '.$token]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl,CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS, $data_to_post);
$data = json_decode(curl_exec($curl));
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
var_dump($data);
如果这不起作用,请告诉我。
我自己解决了这个问题。必须使用 php curl file create 来使其工作而不是附加 '@'
如何使用 PHP cURL
上传文件<?php
// Helper function courtesy of https://github.com/guzzle/guzzle/blob/3a0787217e6c0246b457e637ddd33332efea1d2a/src/Guzzle/Http/Message/PostFile.php#L90
function getCurlValue($filename, $contentType, $postname)
{
// PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
// See: https://wiki.php.net/rfc/curl-file-upload
if (function_exists('curl_file_create')) {
return curl_file_create($filename, $contentType, $postname);
}
// Use the old style if using an older version of PHP
$value = "@{$this->filename};filename=" . $postname;
if ($contentType) {
$value .= ';type=' . $contentType;
}
return $value;
}
$filename = '/path/to/file.jpg';
$cfile = getCurlValue($filename,'image/jpeg','cattle-01.jpg');
//NOTE: The top level key in the array is important, as some apis will insist that it is 'file'.
$data = array('file' => $cfile);
$ch = curl_init();
$options = array(CURLOPT_URL => 'http://your/server/api/upload',
CURLOPT_RETURNTRANSFER => true,
CURLINFO_HEADER_OUT => true, //Request header
CURLOPT_HEADER => true, //Return header
CURLOPT_SSL_VERIFYPEER => false, //Don't veryify server certificate
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
$header_info = curl_getinfo($ch,CURLINFO_HEADER_OUT);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = substr($result, $header_size);
curl_close($ch);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>File Upload results</title>
</head>
<body>
<p>Raw Result: <?=$result?>
<p>Header Sent: <?=$header_info?></p>
<p>Header Received: <?=$header?></p>
<p>Body: <?=$body?></p>
</body>
</html>