【问题标题】:Inserting Query String Variables in XML using PHP/curl使用 PHP/curl 在 XML 中插入查询字符串变量
【发布时间】:2012-06-26 23:18:21
【问题描述】:

我正在尝试从我们网站上的表单向我们的 CRM 发送一些信息,但在将变量插入 XML 时遇到了困难。这是我的代码的简化版本。请注意我试图在 XML 变量中插入 $email 变量的位置......这是行不通的。

<?php

$email = $_GET["email"];


$xml = '<xmlrequest>
<details>
    <emailaddress>$email</emailaddress>
    <mailinglist>8</mailinglist>
    <format>html</format>
    <confirmed>no</confirmed>
</details>
</xmlrequest>
';

 $ch = curl_init('http://mysite.com/xml.php');
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
 $result = @curl_exec($ch);
 if ($result === false) {
echo "Error performing request";
 } else {
$xml_doc = simplexml_load_string($result);
header( "Location: http://mysite.com/confirmation?email=$email" ) ;

//echo 'Status is ', $xml_doc -> status, '<br/>';
 if ($xml_doc -> status == 'SUCCESS') {
    echo 'Data is ', $xml_doc -> data, '<br/>';

} else {
    echo 'Error is ', $xml_doc -> errormessage, '<br/>';
}
}

?>

如果我只是输入 API 的电子邮件地址值,则可以正常工作。但是,我对如何从 PHP 变量中动态提取它一无所知。非常感谢任何帮助!

【问题讨论】:

  • 发送标头重定向后,您将不会看到 echo..
  • 我认为仔细检查代码可以解决这里的问题...

标签: php xml api http curl


【解决方案1】:

字符串定义错误 用这个

$xml = "<xmlrequest>
<details>
    <emailaddress>{$email}</emailaddress>
    <mailinglist>8</mailinglist>
    <format>html</format>
    <confirmed>no</confirmed>
</details>
</xmlrequest>";

或者这个

$xml = '<xmlrequest>
<details>
    <emailaddress>' . $email . '</emailaddress>
    <mailinglist>8</mailinglist>
    <format>html</format>
    <confirmed>no</confirmed>
</details>
</xmlrequest>';

因为这个变量可能是各种字符串,我认为最好在电子邮件周围使用&lt;![CDATA[]]&gt; 部分。

【讨论】:

  • PazsitZ - 你就是那个男人......第二个 sn-p 就像一个魅力。干杯!
猜你喜欢
  • 2016-04-24
  • 2021-02-28
  • 1970-01-01
  • 2012-06-14
  • 2014-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多