【问题标题】:Embed XML in JSON POST to PHP-CURL在 JSON POST 中嵌入 XML 到 PHP-CURL
【发布时间】:2016-05-17 13:27:48
【问题描述】:

我需要以以下 JSON 格式发布 REST 调用。请注意,有效负载变量是 XML。我正在使用带有 CURL 的 PHP 5。寻找我做错了什么。谢谢

{
    "processDefId":"testing~SimpleApplication!1.0~SimpleProcess",
    "serviceName":"SimpleProcess.service",
    "operation":"start",
    "payload":"<payload><formArg><p0:WebForm1 xmlns:p0=\"http://www.frevvo.com/schemas/_N5J6IBUOEeWgf4K8GSSanA\"/></formArg></payload>", 
    "action":"Submit"
}

构建了以下内容,但没有得到响应,也没有看到任何对被调用应用程序的操作。

try {
    echo '<br>Curl Start<br>';
    echo file_put_contents($log, "Start \n", FILE_APPEND);

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL,"http://192.168.56.151:7003/bpm/api/3.0/processes");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_FAILONERROR);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json') );
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json') );
    curl_setopt($curl, CURLOPT_USERPWD, "jstein:welcome1");

    $xml  = '<ns1:start xmlns:ns1=\http://xmlns.oracle.com/bpm/webform/formsData/requestTravelAccount\">';
    $xml .=  '<formArg    xmlns:ns2=\"http://www.frevvo.com/schemas/_1g73gMt3EeWsKsy3N_19_Q\">';
    $xml .=    '<ns2:RequestTravelAccount>';
    $xml .=       '<EmployeeId>1771</EmployeeId>';
    $xml .=       '<FirstName>Aaron</FirstName>';
    $xml .=       '<LastName>Thompson</LastName>';
    $xml .=       '<PersonId>300000087953021</PersonId>';
    $xml .=    '</ns2:RequestTravelAccount>';
    $xml .=  '</formArg>';
    $xml .= '</ns1:start>';
    $xml_e = htmlspecialchars($xml, ENT_QUOTES);
    echo file_put_contents($log, $xml_e ."\n", FILE_APPEND);

    $post_data = array(    
        "processDefId"=>"testing~HCM!1.0~NewEmployeeTravelSetupProcess",
        "serviceName"=>"NewEmployeeTravelSetupProcess.service",
        "operation"=>"start",
        "action"=>"Submit",
        "payload" => $xml_e 
    );
    $curl_post_data = json_encode($post_data);
    echo file_put_contents($log, "$curl_post_data"."\n", FILE_APPEND);

    curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);

    $curl_response = curl_exec($curl);
    echo '<br> CURL_EXEC return: ' . curl_error($curl);
} catch (Exception $e) {

    echo 'Caught exception" ',$e->getMessage(), "\n";
}
curl_close($curl);

echo '<br>CURL done';

【问题讨论】:

    标签: php json xml curl


    【解决方案1】:

    您正在初始化CURLOPT_HTTPHEADER 选项两次。而您只需要使用所有标题初始化一次。

    错误:

    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json') );
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json') );  // resetting above one for this line.
    

    正确:

    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json') );
    

    【讨论】:

      猜你喜欢
      • 2018-06-13
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 1970-01-01
      • 2014-12-13
      • 2015-04-02
      • 2015-08-26
      • 1970-01-01
      相关资源
      最近更新 更多