【问题标题】:Post .xml file automatically to REST API将 .xml 文件自动发布到 REST API
【发布时间】:2012-08-12 07:35:20
【问题描述】:

我们需要将 .xml 文件发布到 API - REST API - BigCommerce。

这是一个比较基础的api..

我们已经尝试过这个 php curl 脚本将 xml 文件发布到 API,但是没有成功。

<?php

// test XML API POST
$filename = "test.xml"; 
$handle = fopen($filename, "r"); 
$XPost = fread($handle, filesize($filename));
fclose($handle);

$url = "https://urlofapi"; // set REST URL 
$api_token = "apihashkey";
$xml = urlencode($XPost);
$user_agent = "SoEasy REST API Client 0.1";

// Get the curl session object 
$session = curl_init($url);
// set url to post to curl_setopt($session, CURLOPT_URL,$url); 

curl_setopt($session, CURLOPT_POST, true); // Tell curl to use HTTP POST; 
curl_setopt($session, CURLOPT_POSTFIELDS, $XPost); // Tell curl that this is the body of the POST 
curl_setopt($session, CURLOPT_USERPWD, $api_token); // define userpassword api token
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_ANY); // defining REST basic authentication 
curl_setopt($session, CURLOPT_HTTPHEADER, Array("Content-Type: application/xml")); // define header 
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false); // ignore ssl cert for debug purposes curl_setopt($session, CURLOPT_USERAGENT, $user_agent); // user agent so the api knows what for some unknown reason 
curl_setopt($session, CURLOPT_HEADER, 1); // Tell curl not to return headers, but do return the response 
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // allow redirects curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);

$response = curl_exec($session);
print_r($response);
curl_close($session);
?>

到目前为止已经连接,但没有成功-出现错误-

Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/user/public_html/test/new.php on line 7

HTTP/1.1 405 Method Not Allowed Date: Sun, 12 Aug 2012 07:31:11 GMT 

Server: Apache Allow: GET, HEAD, OPTIONS
X-BC-ApiLimit-Remaining: 5000 X-BC-Store-Version: 7.3.37 
X-Powered-By: PleskLin Transfer-Encoding: chunked Content-Type: application/xml 
X-Pad: avoid browser bug 405 This resource does not support the requested method.
 Refer to the Allow response header for methods supported by this resource.

我们只需将一个 .xml 文件发布到大型商务 API 即可。 .xml 来自帐户软件并生成正确的 xml。

【问题讨论】:

  • 它是标准的 xml curl Bigcommerce REST api。

标签: php xml curl


【解决方案1】:

我知道这是一篇较旧的帖子 - 问题是 OP 在检查其长度时引用了文件名,而不是文件处理程序,因此每次都得到 0。

$XPost = fread($handle, filesize($filename));

应该是:

$XPost = fread($handle, filesize($handle));

【讨论】:

    【解决方案2】:

    文件test.xml 的长度为零或(可能更多情况)不存在。

    您需要启用 PHP 错误日志,然后跟踪错误日志以了解您遇到的此类问题或进行实际错误检查。

    此外,您正在向不支持它的服务器端点发送 POST 请求。您需要了解 HTTP 协议基础知识才能正确理解给定的错误消息以及提供的调试信息。

    【讨论】:

    • Test.xml 不是空文件,我已经检查过了。 PHP 错误日志记录已启用,并且我检查了 error_log 中的错误。来自代码的调试也是 php display_errors。它所需要做的只是将一个 .xml 文件发布到 bigcommerce API。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 2011-12-15
    • 2012-06-17
    相关资源
    最近更新 更多