0

我正在尝试通过 PHP 的 CURL 将配置文件通过 POST 上传到 Cisco APIC-EM,但收到意外错误:

//File Upload
echo "<b>File Upload:</b><br>";
$namespace = "config";
$data = array(
        "version" => "",
        "response" => 
        array(
            "nameSpace" => $namespace,
            "encrypted" => false,
            "id" => "",
            "md5Checksum" => "",
            "sha1Checksum" => "",
            "fileFormat" => "",
            "fileSize" => "",
            "downloadPath" => "http://hama0942.global.bdfgroup.net/network/net_config/bdfbrsao00000000rt02.txt",
            "name" => "bdfbrsao00000000rt02.txt",
            "attributeInfo" => "object"
            )
        );
$data = json_encode($data);
//For Debugging only - Request
echo "<br>Request:<br>";
echo '<pre>';
print_r($data);
echo '</pre>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, "https://hams1484.global.bdfgroup.net/api/v1/file/".$namespace);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-    data", "X-Auth-Token: ".$serviceTicket));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array($data));
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$response = curl_exec($ch);
echo "<br><b>Request-Header: </b>".curl_getinfo($ch, CURLINFO_HEADER_OUT )."<br><br>";
//For Output Debugging only - Response
echo "<br>Response:<br>";
echo '<pre>';
print_r(json_decode($response));
echo '</pre>';
curl_close($ch);
echo "<br><hr>";

这是结果:

File Upload:

Request:

{"version":"","response":{"nameSpace":"config","encrypted":false,"id":"","md5Checksum":"","sha1Checksum":"","fileFormat":"","fileSize":"","downloadPath":"http:\/\/hama0942.global.bdfgroup.net\/network\/net_config\/bdfbrsao00000000rt02.txt","name":"bdfbrsao00000000rt02.txt","attributeInfo":"object"}}


Request-Header: POST /api/v1/file/config HTTP/1.1 Host: hams1484.global.bdfgroup.net Accept: */* X-Auth-Token: ST-1111-OUiNBLtgALg2ufcwFrh5-cas Content-Length: 436 Expect: 100-continue Content-Type: multipart/form-data; boundary=------------------------5e86cb7113449960


Response:

stdClass Object
(
[response] => stdClass Object
    (
        [errorCode] => 7062
        [message] => Unexpected error
        [detail] => Non valid file object
    )

[version] => 1.0
)

恐怕 CURL 设置有问题,但我不确定。有人知道这里有什么问题吗?我正在使用 PHP7。

谢谢。

4

1 回答 1

0

我已经为此苦苦挣扎了很长时间,终于找到了解决方案。

1:上传的文件必须是您正在卷曲的服务器上的本地文件。

2 : $file = realpath("filenameOnServer");

3 : $config = new CURLfile($file);

4 : $array = array("fileUpload" => $config); $array = json_encode($array);

5 : curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: multipart/form-data; charset=utf-8;', 'X-Auth-Token: ' . $token,
));

6:将数组作为 CURL 中的 POST 变量发送。不需要其他任何东西

于 2017-05-12T10:19:13.707 回答