Curl请求可在Command中使用,而不能在PHP中[保留]

问题描述 投票:-3回答:1

首先,我研究了可能的重复项,但似乎都没有帮助。因此,我提出了新的问题。我有这个完全正常的curl命令:

C:\wamp64\www\PassportPDF\SVGtoPDF>curl -X POST "https://my.api.com" -H "accept: text/plain" -H "Content-Type: multipart/form-data" -F "[email protected];type=image/svg+xml"

然后我尝试将其移植到PHP,无法使其正常工作。

function load2($url, $token, $file ) {

    $filename =  $file['name'];
    $filedata =  $file['tmp_name'];
    $filesize =  $file['size'];

    $filedata = file_get_contents($filedata);

    if ($filedata != '')
    {
        $headers = array(
            "accept: text/plain",
            "Content-Type: multipart/form-data",
        ); // cURL headers for file uploading
        $postfields = array(
            "fileData" => "@settings.svg;type=image/svg+xml"
        );
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_HEADER,true);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$postfields);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        var_dump("\$filesize:".$filesize);
        $response = curl_exec($ch);
        var_dump(">>>".$response);
        if(!curl_errno($ch))
        {
            var_dump("1");
            $info = curl_getinfo($ch);
            if ($info['http_code'] == 200)
                $errmsg = "File uploaded successfully";
        }
        else
        {
            var_dump("2");
            $errmsg = curl_error($ch);
            var_dump("???".$errmsg);
        }
        curl_close($ch);
    }
    else
    {
        $errmsg = "Please select the file";
    }
} 

我总是以错误500结尾。我想念什么?

php curl post multipart form-data
1个回答
0
投票

您正在用字符串替换$postfields变量。原始数组格式是正确的,应该可以使用。

您在哪里设置$filesize

© www.soinside.com 2019 - 2024. All rights reserved.