Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用API发送XML post请求_Php_Xml_Api_Curl_Cloud - Fatal编程技术网

Php 使用API发送XML post请求

Php 使用API发送XML post请求,php,xml,api,curl,cloud,Php,Xml,Api,Curl,Cloud,我试图发出一个xml post请求,该请求会命中另一个服务器url并发布其xml数据,但这里的代码不适合我,这是我的代码 if ($mail->send()) { $URL = "server link api based"; $ch = curl_init($URL); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VER

我试图发出一个xml post请求,该请求会命中另一个服务器url并发布其xml数据,但这里的代码不适合我,这是我的代码

if ($mail->send()) {
        $URL = "server link api based";

        $ch = curl_init($URL);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml','Content-Length:     ' . strlen($xml)));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
      //  echo $output;
        if ($output) {
            echo "1";
        } else {
            echo "2";
        }
    }
} catch (Exception $e) {
    echo $e;
}
数据的XML格式这里是XML格式的代码

$xml = '<?xml version="1.0" encoding="UTF-8"?>
    <data>
    <lead>
       <key>YaskjdfweuYksdlksid....</key>
    <leadgroup>.....</leadgroup>
    <site>.....</site>
    <introducer>0</introducer>

    <source>FDH Quick submission</source>
    <medium>FDH Quick submission</medium>

          <firstname>' . $firstName . '</firstname>
          <lastname>' . $lastName . '</lastname>
          <email>' . $userEmail . '</email>
          <postcode>' . $postCode . '</postcode>
          <contactphone>Yes</contactphone>
          <phone1>' . $userPhoneNo . '</phone1>
   </lead>
</data>';
$xml='1!'
Yaskjdfweyksdlksid。。。。
.....
.....
0
外佣速递
外佣速递
' . $名字。”
' . $姓。”
' . $用户电子邮件。”
' . $邮政编码。”
对
' . $用户电话号码
';

1可能的问题是在XML中插入字符串时没有转义字符串,这可能会导致XML文件损坏(例如,
&
必须转义为
什么“不起作用”对您意味着什么?请添加更多详细信息,代码不发送、发送不正确等。此外,我不认为您没有先
try
ing就
catch
。可能还希望在这些变量上使用htmlspecialchars,以便正确编码特殊字符。不起作用意味着,当我想在其他门户上发布数据时,数据不起作用出现在那里,代码中没有错误,因为我还添加了带有此cod的电子邮件,以测试接收基于模板的电子邮件,但电子邮件工作正常电子邮件中没有错误xml中没有错误但代码中有一个问题数据不在我要发布的服务器上,代码运行成功但未将数据放入服务器我们可以测试xml格式吗n测试网站,使用该网站的api测试发布数据的任何链接server@adilamin我不知道有任何预先制作的测试网站,但你可以很容易地为自己创建一个。这简直就是
@hanshenrik DUDE你是一个救命稻草!!!…我花了30个小时追逐我的尾巴,所有这些都是无效的XML…非常感谢你!!!
function xml_esacpe(string $str, string $charset = 'UTF-8'): string {
    if (! strlen ( $str )) {
        return "";
    }
    $ret = htmlspecialchars ( $str, ENT_QUOTES | ENT_SUBSTITUTE | ENT_DISALLOWED | ENT_XML1, $charset, true );
    if (! strlen ( $ret )) {
        throw new Exception ( "failed to xml-encode input! (most likely wrong charset or corrupted input)" );
    }
    return $ret;
}