如何通过CURL和PHP执行文件上传

时间:2019-05-30 10:00:31

标签: php curl

我一直在创建一个脚本,以将数据从JSON文件插入到POST请求中。

虽然它执行所需的操作以下载提供的URL中的图像,而不是下载并在我的路线上执行发布请求。

过去的几个小时,我一直在努力使其正常运行,但没有成功。

我使用了 realpath 和CurlFile,但是它没有起作用。

这是请求:

<?php
$image = $this->makeCurlFile(realpath("scorpion_stretch-59788999e4432d80.jpg"));

$body = ["name" => "uploaded_file", "filename" => $image->name];

echo print_r($body);

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://resources.diamondappgroup.com/upload.php",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => array(
        "Accept: */*",
        "Cache-Control: no-cache",
        "Connection: keep-alive",
        "Content-Type: application/x-www-form-urlencoded",
        "Host: resources.diamondappgroup.com",
        "Postman-Token: 45ebd192-9621-482c-8735-2061bc31c6fa,6a7a959f-372d-4afb-9efa-6b4da47ea1b0",
        "User-Agent: PostmanRuntime/7.11.0",
        "accept-encoding: gzip, deflate",
        "cache-control: no-cache",
        "content-length: 312659",
        "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}

0 个答案:

没有答案