简体   繁体   English

CURLOPT_POSTFIELDS

[英]CURLOPT_POSTFIELDS

I am looking to post an xml file to a web service running on a sbs server but it requires 2 postfields. 我希望将xml文件发布到在sbs服务器上运行的Web服务,但是它需要2个postfield。 A "userid" and "secret". 一个“用户ID”和“秘密”。 I have it setup in an array but i'm missing something as its failing. 我将其设置在一个数组中,但由于它失败而丢失了一些东西。 I have tried the clientid & secret in the url string but the developer has requested it as a post field. 我已经尝试了URL字符串中的clientid和secret,但是开发人员已将其请求为post字段。 Here is what i have already. 这是我已经拥有的。

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;   
}

I found out the problem in my code. 我在代码中发现了问题。 Below is how i fixed it should anyone else have the same problem. 下面是我如何解决其他任何人也有同样问题的方法。

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