doutang0335 2012-06-26 23:18
浏览 53
已采纳

使用PHP / curl在XML中插入查询字符串变量

I'm trying to send some information to our CRM from a form on our site and am getting stuck on inserting the variables into the XML. Here is a simplified version of my code. Notice where I'm trying to insert the $email variable within the XML variable...which is not working.

<?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/>';
}
}

?>

If I just type in an email address value for the API works fine. However, I'm clueless on how to pull this in dynamically from a PHP variable. Any help is greatly appreciated!

  • 写回答

1条回答 默认 最新

  • drh78568 2012-06-26 23:47
    关注

    The string definition is bad use this

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

    or this

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

    Because this variable probably can be various string I think it's better if you use <![CDATA[]]> section around the email.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测