使用curl通过php上传文件到cisco APIC-EM

时间:2017-03-15 13:20:34

标签: php curl cisco apic

我正在尝试通过POST将配置文件通过PHP的CURL上传到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。

感谢。

1 个答案:

答案 0 :(得分:0)

我一直在努力解决这个问题,最后找到了解决方案。

1:上传文件必须是您正在进行CURL的服务器上的本地文件。

2:$ file = realpath(“filenameOnServer”);

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

4:$ array = array(“fileUpload”=&gt; $ config); $ array = json_encode($ array);

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

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