簡體   English   中英

CURLOPT_POSTFIELDS

[英]CURLOPT_POSTFIELDS

我希望將xml文件發布到在sbs服務器上運行的Web服務,但是它需要2個postfield。 一個“用戶ID”和“秘密”。 我將其設置在一個數組中,但由於它失敗而丟失了一些東西。 我已經嘗試了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;   
}

我在代碼中發現了問題。 下面是我如何解決其他任何人也有同樣問題的方法。

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;


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM