1

We need to post an .xml file to an API - REST API - BigCommerce.

It's a relatively basic api..

We have tried this php curl script posting the xml file to the API however no luck.

<?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);
?>

This has so far connected, but not successfully - an error occurs -

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.

All we need to simply do is post a .xml file to the big commerce API. The .xml is coming from account software and produces the correct xml.

1
  • Its the standard xml curl Bigcommerce REST api. Commented Aug 12, 2012 at 10:28

2 Answers 2

1

I know this is an older post - the issue was that the OP referenced the file name, not file handler, when checking its length and thus got 0 each time.

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

should have been:

$XPost = fread($handle, filesize($handle));
Sign up to request clarification or add additional context in comments.

Comments

1

The file test.xml is of zero length or (probably more the case) does not exist.

You need to enable PHP error logging and then track the error log to learn about such problems in your case or do actual error checking.

Additionally you are sending a POST request to a server endpoint that does not support it. You here need to learn about HTTP protocol basics to properly understand the given error message as well as the provided debug information.

1 Comment

Test.xml is not a empty file, ive checked. PHP error logging is enabled, and ive checked for errors within error_log. Also the debug that came from the code, is of php display_errors. All it needs to do is simply post an .xml file to the bigcommerce API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.