【问题标题】:Upload file via php to cisco APIC-EM using curl使用 curl 通过 php 将文件上传到 cisco APIC-EM
【发布时间】:2017-03-15 13:20:33
【问题描述】:

我正在尝试通过 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。

谢谢。

【问题讨论】:

    标签: php curl cisco apic


    【解决方案1】:

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

    1 : 上传文件必须是 CURL 所在服务器上的本地文件。

    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-令牌:' 。 $令牌,
    ));

    6 : 将数组作为 CURL 中的 POST 变量发送。其他都不需要了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-24
      • 2016-02-22
      • 2017-10-25
      • 2016-03-19
      • 1970-01-01
      • 2011-08-04
      • 1970-01-01
      • 2017-04-30
      相关资源
      最近更新 更多