【发布时间】:2017-12-14 23:09:59
【问题描述】:
这个问题是市场在这个线程中回答的:
How to POST an XML file using cURL on php?
但在我看来,这个答案并不是真正的正确答案,因为它只是展示了如何使用 cURL 发送 XML 代码。我需要发送一个 XML 文件。
基本上,我需要将这段C#代码转换成PHP:
public Guid? UploadXmlFile()
{
var fileUploadClient = new WebClient();
fileUploadClient.Headers.Add("Content-Type", "application/xml");
fileUploadClient.Headers.Add("Authorization", "api " + ApiKey);
var rawResponse = fileUploadClient.UploadFile(Url, FilePath);
var stringResponse = Encoding.ASCII.GetString(rawResponse);
var jsonResponse = JObject.Parse(stringResponse);
if (jsonResponse != null)
{
var importFileId = jsonResponse.GetValue("ImportId");
if (importFileId != null)
{
return new Guid(importFileId.ToString());
}
}
return null;
}
我尝试了多种方法,这是我最近的尝试。
cURL 调用:
/**
* CDON API Call
*
*/
function cdon_api($way, $method, $postfields=false, $contenttype=false)
{
global $api_key;
$contenttype = (!$contenttype) ? 'application/x-www-form-urlencoded' : $contenttype;
$curlOpts = array(
CURLOPT_URL => 'https://admin.marketplace.cdon.com/api/'.$method,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 60,
CURLOPT_HTTPHEADER => array('Authorization: api '.$api_key, 'Content-type: '.$contenttype, 'Accept: application/xml')
);
if ($way == 'post')
{
$curlOpts[CURLOPT_POST] = TRUE;
}
elseif ($way == 'put')
{
$curlOpts[CURLOPT_PUT] = TRUE;
}
if ($postfields !== false)
{
$curlOpts[CURLOPT_POSTFIELDS] = $postfields;
}
# make the call
$ch = curl_init();
curl_setopt_array($ch, $curlOpts);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
文件导出:
/**
* Export products
*
*/
function cdon_export()
{
global $api_key;
$upload_dir = wp_upload_dir();
$filepath = $upload_dir['basedir'] . '/cdon-feed.xml';
$response = cdon_api('post', 'importfile', array('uploaded_file' => '@/'.realpath($filepath).';type=text/xml'), 'multipart/form-data');
echo '<br>Response 1: <pre>'.print_r(json_decode($response), true).'</pre><br>';
$data = json_decode($response, true);
if (!empty($data['ImportId']))
{
$response = cdon_api('put', 'importfile?importFileId='.$data['ImportId'], false, 'text/xml');
echo 'Response 2: <pre>'.print_r(json_decode($response), true).'</pre><br>';
$data = json_decode($response, true);
}
}
但我得到的输出是这样的:
响应 1:
stdClass 对象 ( [消息] => 请求不包含有效的媒体类型。 )
我在不同的地方尝试了不同的类型,application/xml、multipart/form-data 和 text/xml,但没有任何效果。
我该怎么做才能让它发挥作用?如何使用 cURL 发送 XML 文件?
【问题讨论】:
-
来自 PHP 手册
"CURLOPT_POSTFIELDS: The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path." -
@RamRaider 是的,正如您所见,我就是这样做的。但这行不通。
-
@RamRaider @ 方法在几年前就被弃用了,在 5.5 中变得不可靠,在 5.6 中更加不可靠,在 7.0 中完全停止工作,使用 CURLFile 代替 @
-
@hanshenrik - 我不知道,我还有 php 5.3.2,所以从来没有在手册中读过它:( 升级时间可能