CURLOPT_POSTFIELDS

时间:2018-03-20 14:34:15

标签: php xml curl

我希望将xml文件发布到在sbs服务器上运行的Web服务,但它需要2个postfields。 A"用户ID"并且"秘密"。我把它安装在一个阵列中,但我失去了一些东西。我尝试过clientid& url字符串中的秘密,但开发人员已将其作为帖子字段请求。这就是我已经拥有的。

file = "../5601_RawData/test.csv.gz"
test <- fread(paste0("gzip -d -c ",file," | grep -E '(.+;.+){17}'"))

1 个答案:

答案 0 :(得分:0)

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

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;


}
相关问题