1

Нужно в PHP сэмулировать что-то вроде этого:

curl -X POST http://localhost/config --data-urlencode config@file

Из мануала:

name@filename Загрузить данные из файла filename, перекодировать их и передать методом POST

мой код:

function cUrl ($cmd, $hjson = false, array $data = [], $post = false) {
    $headers[] = 'Content-Type: application/x-www-form-urlencoded';
    if ($hjson)
        $headers[] = 'Accept: application/json; charset=utf-8';
    $curl = curl_init();

    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_URL, $this->url.$cmd);
    curl_setopt($curl, CURLOPT_POST, $post);
    if ($post)
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));

    $res = curl_exec($curl);
    curl_close($curl);
    if ($res === false) {
        throw new Exception(json_encode(error_get_last()));
    }
    return $res;
}

try {
    $res = $this->cUrl('/config',  false, ['config' => '@'.$cfgFile], true);
} catch (Exception $e) {
    echo date("Y/m/d H:i:s").' '.$this->ip.": upload config file fail: ".$e->getMessage().PHP_EOL;
}

пытался по-всякому:

['config' => '@'.$cfgFile]

['config' => $cfgFile]

['config' => '@'.urlencode($cfgFile)]

['config' => urlencode($cfgFile)]

проблема в том, что файл не кодируется, и оборудование его скачивает, но не хочет его применять...

Как сделать правильно?

Генераторы типа incarnate не помогли, там --data-urlencode вообще ничего не меняет...

1 ответ 1

0

получилось так:

    function cUrl ($cmd, $hjson = false, array $data = [], $post = false) {
        ....
        if ($post)
            curl_setopt(
                $curl,
                CURLOPT_POSTFIELDS, 
                http_build_query($data, null, null, PHP_QUERY_RFC3986)
            );
        ....
    }

    try {
        $res = $this->cUrl('/config',  false, ['config' => file_get_contents($cfgFile)], true);
        ....
    }

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.