亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 php 中將 <string xmlns="http://tempuri.org/">

如何在 php 中將 <string xmlns="http://tempuri.org/">

PHP
墨色風雨 2023-04-21 10:51:05
我發現在 php 中轉換<string xmlns="http://tempuri.org/">為 json 有問題,我試圖尋找一個例子但都失敗了。這是我的 PHP 腳本:public function register() {        $url = 'https://example.com/register';        $post_data="[email protected]";        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_POST, true);        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded; charset=utf-8'));           curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);         $result = curl_exec($ch);        echo $result;    }這是回應:請任何人幫我解決這個問題。
查看完整描述

3 回答

?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

在 XML 文檔中發送 JSON 是一個奇怪的想法,但無論如何。最簡單(明智的方法)是用 SimpleXML 加載它,然后 JSON 只是根節點的文本......


$xml = simplexml_load_string($result);

echo (string)$xml;

應該給


[

    {

    "CreateDate": "123"

    }

]


查看完整回答
反對 回復 2023-04-21
?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

在 XML 中嵌入 JSON 很奇怪,但 IMO 他的解決方案在假設輸入簡單的情況下發揮得有點快和松散。此外,就 PHP 的 XML 庫而言,DOMDocument 和系列往往更易于使用。


$in = <<<_E_

<?xml version="1.0" encoding="utf-8"?>

<string xmlns="http://tempuri.com/">[

? ? {

? ? ? ? "foo": "bar"

? ? }

]</string>

_E_;


$doc = new DomDocument();

$doc->loadxML($in);

$json = $doc->getElementsByTagName('string')->item(0)->nodeValue;

$decoded = json_decode($json, true);


var_dump($json, $decoded);

輸出:


string(24) "[

? ? {

? ? ? ? "foo": "bar"

? ? }

]"


array(1) {

? [0]=>

? array(1) {

? ? ["foo"]=>

? ? string(3) "bar"

? }

}


查看完整回答
反對 回復 2023-04-21
?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

最后,我找到了一個解決方案,如下所示:


public function register() {

        $url = 'https://example.com/register';


        $post_data="[email protected]";


        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, true);

        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded; charset=utf-8'));   

        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

        $result = curl_exec($ch);


        $xml = simplexml_load_string($result,'SimpleXMLElement',LIBXML_NOCDATA);

        header('Content-Type: application/json');

        $temp = json_decode($xml);

        $json = json_encode($temp[0]);

        echo $json;

    }

它有效;



查看完整回答
反對 回復 2023-04-21
  • 3 回答
  • 0 關注
  • 156 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號