如何使用PHP cURL发送XML文件?

时间:2017-12-14 23:09:59

标签: php xml curl

这个问题是市场在这个帖子中回答:

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对象 (     [留言] =&gt;请求不包含有效的媒体类型。 )

我在不同的地方尝试了不同的类型application/xmlmultipart/form-datatext/xml,但没有任何效果。

如何让它发挥作用?如何设置使用cURL发送XML文件?

1 个答案:

答案 0 :(得分:1)

对我来说,看起来C#代码只相当于

function UploadXmlFile(): ?string {
    $ch = curl_init ( $url );
    curl_setopt_array ( $ch, array (
            CURLOPT_POST => 1,
            CURLOPT_HTTPHEADER => array (
                    "Content-Type: application/xml",
                    "Authorization: api " . $ApiKey 
            ),
            CURLOPT_POSTFIELDS => file_get_contents ( $filepath ),
            CURLOPT_RETURNTRANSFER => true 
    ) );
    $jsonResponse = json_decode ( ($response = curl_exec ( $ch )) );
    curl_close ( $ch );
    return $jsonResponse->importId ?? NULL;
}

但至少有1个不同,你的PHP代码添加了标题'Accept: application/xml',你的C#代码没有