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

时间:2019-04-02 03:24:11

标签: php curl php-curl

以下命令的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);

2 个答案:

答案 0 :(得分: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 :(得分:-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);
相关问题