Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将.xml文件自动发布到RESTAPI_Php_Xml_Curl - Fatal编程技术网

Php 将.xml文件自动发布到RESTAPI

Php 将.xml文件自动发布到RESTAPI,php,xml,curl,Php,Xml,Curl,我们需要将一个.xml文件发布到API-RESTAPI-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); $u

我们需要将一个.xml文件发布到API-RESTAPI-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);
?>

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

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

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


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

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

$XPost = fread($handle, filesize($filename));
应该是:

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

这是标准的xml文件。我检查过,Test.xml不是一个空文件。PHP错误日志已启用,我已在错误日志中检查错误。另外,来自代码的调试是php显示错误。它所需要做的只是将一个.xml文件发布到BigCommerceAPI。
$XPost = fread($handle, filesize($handle));