【问题标题】:CURLOPT_POSTFIELDSCURLOPT_POSTFIELDS
【发布时间】:2018-03-20 14:34:15
【问题描述】:

我希望将 xml 文件发布到在 sbs 服务器上运行的 Web 服务,但它需要 2 个后域。一个“用户名”和“秘密”。我将它设置在一个数组中,但由于它的失败,我遗漏了一些东西。我已经在 url 字符串中尝试了 clientid 和 secret,但开发人员已将其作为 post 字段请求。这是我已经拥有的。

add_action('gform_after_submission_73', 'post_to_third_party', 10, 2);

function post_to_third_party($entry)
{
    $url      = 'http://mywebsitesite.co.uk/webdata.aspx';
    $id = array('clientid' => '15', 'secret' => '123');
    $encoding = 'WINDOWS-1252';
    $brand    = htmlspecialchars($entry['20'], ENT_XML1, $encoding);
    $product  = htmlspecialchars($entry['22'], ENT_XML1, $encoding);
    $form_id  = htmlspecialchars($entry['21'], ENT_XML1, $encoding); 

    $xml = "<?xml version=\"1.0\" encoding=\"$encoding\"?>
    <webform>
        <brand>$brand</brand>
        <product>$product</product>
        <form_id>$form_id</form_id>
    </webform>";

     $ch = curl_init($url);

    if ($ch === false) {
        throw new RuntimeException("Unable to initialise a session"); 
    }

    $result = curl_setopt_array($ch, [
        CURLOPT_POST => 1,
        CURLOPT_HTTPHEADER => ['Content-Type: text/xml'],
        CURLOPT_POSTFIELDS => $xml,
        CURLOPT_URL => $url,
        CURLOPT_POSTFIELDS => $id,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_SSL_VERIFYHOST => 2,
        CURLOPT_SSL_VERIFYPEER => 1,
    ]);

    if ($result === false) {
        throw new RuntimeException("Unable to set session options");
    }

    $output = curl_exec($ch);

    if ($output === false) {
        throw new RuntimeException("Request failed: " . curl_error($ch));
    }

    curl_close($ch);

    echo $output;   
}

【问题讨论】:

  • 您只提供一次 POST 字段。但是你有CURLOPT_POSTFIELDS =&gt; $xml,CURLOPT_POSTFIELDS =&gt; $id,;删除前者并将 $xm 放入 $id 数组中并传递它

标签: php xml curl


【解决方案1】:

我在我的代码中发现了问题。如果其他人有同样的问题,下面是我如何修复它。

add_action('gform_after_submission_73', 'post_to_third_party', 10, 2);

function post_to_third_party($entry)
{
    $url      = 'http://mywebsitesite.co.uk/webdata.aspx';  
    $user   = '15';
    $pass   = '123';
    $encoding = 'WINDOWS-1252';
    $brand    = htmlspecialchars($entry['20'], ENT_XML1, $encoding);
    $product  = htmlspecialchars($entry['22'], ENT_XML1, $encoding);
    $form_id  = htmlspecialchars($entry['21'], ENT_XML1, $encoding);


    $xml = "<?xml version=\"1.0\" encoding=\"$encoding\"?>
    <webform>
        <brand>$brand</brand>
        <product>$product</product>
        <form_id>$form_id</form_id>
    </webform>";


    $ch = curl_init($url);

    if ($ch === false) {
        throw new RuntimeException("Unable to initialise a session"); 
    }


$xmldata = array( 'clientid' => $user, 'secret' => $pass, 'data' => $xml);
$params = http_build_query($xmldata);

    $result = curl_setopt_array($ch, [
        CURLOPT_POST => 1,
        CURLOPT_URL => $url,            
        CURLOPT_POSTFIELDS => $params,
        CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded;'],
        //CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_SSL_VERIFYHOST => 2,
        CURLOPT_SSL_VERIFYPEER => 1,
    ]);

    if ($result === false) {
        throw new RuntimeException("Unable to set session options");
    }

    $output = curl_exec($ch);

    if ($output === false) {
        throw new RuntimeException("Request failed: " . curl_error($ch));
    }

    curl_close($ch);

    echo $output;


}

【讨论】:

    猜你喜欢
    • 2017-12-08
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 2020-11-27
    相关资源
    最近更新 更多