以下curl命令的php curl版本是什么?

问题描述 投票:0回答:2

什么是以下命令的php curl版本

curl -X PUT -H 'Content-Type: text/csv' -H 'Accept: application/json' -d @file.csv [URL]

我有下面的php curl版本,但不起作用

$headers = [
   'Content-Type: text/csv',
   'Accept: application/json'
];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filename));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
php curl php-curl
2个回答
2
投票

试试下面的代码

$headers = [
   'Content-Type: text/csv',
   'Accept: application/json'
];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, ''.$url'');
curl_setopt($ch, CURLOPT_PUT, 1);

$fp = fopen($file_path, 'r');

curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response= curl_exec ($ch);
fclose($fp);


-1
投票

有一个工具可以将cURL命令转换为cURL的PHP​​版本。你可以试试这个网站:curl-toPHP

这是使用cURL命令的示例输出。

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'your-post-remote');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post = array(
    'file' => '@' .realpath('file.csv')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');


$headers = array();
$headers[] = 'Content-Type: text/csv';
$headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
© www.soinside.com 2019 - 2024. All rights reserved.