Что такое php curl версия приведенной ниже команды curl - PullRequest
0 голосов
/ 02 апреля 2019

Какая версия 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 ]

2 голосов
/ 02 апреля 2019

Попробуйте код ниже

$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);

0 голосов
/ 02 апреля 2019

Существует инструмент для преобразования команды cURL в PHP-версию cURL. Вы можете попробовать этот сайт: 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);
...