【问题标题】:extract data from file_get_contents从 file_get_contents 中提取数据
【发布时间】:2020-11-03 21:25:20
【问题描述】:

我已使用 webex 数据在我的网站上显示会议,但是在返回数据时遇到了问题。

$sitename = "sitename";       
$username = "api@webex.com";
$password = "password!";

$XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$XML .="<serv:message xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
$XML .="        <header>";
$XML .="                <securityContext>";
$XML .="                        <webExID>$username</webExID>";
$XML .="                        <password>$password</password>";
$XML .="                        <siteName>$sitename</siteName>";

$XML .="                </securityContext>";
$XML .="        </header>";
$XML .="        <body>";
$XML .="                <bodyContent xsi:type=\"java:com.webex.service.binding.meeting.GetjoinurlMeeting\">";
$XML .="                <meetingKey>1460755337</meetingKey>";
$XML .="                </bodyContent>";
$XML .="        </body>";
$XML .="</serv:message>";

$request = stream_context_create(array("http"=>array("method"=>"POST","header"=>"Content-Type:text/xml","content"=>$XML)));

$response = file_get_contents("https://$sitename.webex.com/WBXService/XMLService", true, $request);

实际的 XML 响应 在传递标头请求后我得到如下响应

SUCCESSPRIMARYfalsefalseC!sco123falsefalseMeeting 13 jjb87@miami.edufalse1myemail@gmail.commyemail@gmail.comPERSONALmyemail@gmail.comVISITOR1097099526INVITE1467275084ENGLISHATTENDEE1truetruetruetruetruetruetruetruetruetruefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsetruetruetruefalsefalsetruefalsetruefalsefalsetruetruefalsefalsefalsetruefalsefalsetruefalsefalsefalsefalsefalsefalsefalsetruefalsefalsefalsetruefalsefalsefalsetruetruetrue07/10/2020 12:00:004GMT-08:00, Pacific (San Jose)20900rachel@modiht.comfalsefalse000falsefalsefalseCALLBACK50falsefalseUS+1-415-655-00011-844-621-3956falsefalseNO_REPEATtruerachel@modiht.comtruefalse0015falsefalsefalse0falsefalse146727508475c9ca6757090f047a57136a85ee09cbNOT_INPROGRESSfalsefalsefalse50223710221954770ec6374b4ace66655ed33b7bcf0a9ac61019001truehttps://api.webex.com/modiht/j.php?MTID=m32de914bf4153f200c60e1ad420376ed1467275084@api.webex.com1467275084@api.webex.com

所以我的问题是如何从回复中获得最后一个1467275084@api.webex.com

请帮我解决这个问题。

【问题讨论】:

  • 看看$response的实际内容(最简单的方法是查看输出页面的源代码),您可能会发现其中有浏览器将其视为HTML的XML标签标签。
  • PHP 有多种工具可以用来从 XML 响应中获取所需的数据。查看stackoverflow.com/questions/3577641/…
  • 你能告诉我我该怎么做的例子吗? @NigelRen
  • 我链接的帖子中有一般示例,但我认为没有人能够告诉您如何在不查看响应 XML 的情况下从响应中获取所需的确切数据。
  • 我已经用 XML 响应编辑了我的问题

标签: php regex xml file-get-contents webex


【解决方案1】:

好的,我在预赛中做到了,这是我问题的答案

$emails = getLastEmailFromString($response);
$arrEmail = implode("\n", $emails);
$lastEMail = end( $emails);
return $lastEMail;

function getLastEmailFromString($string){
  preg_match_all("/[\._a-zA-Z0-9-]+@[\.api.webex.com-]+/i", $string, $matches);
  return $matches[0];
}

【讨论】:

    【解决方案2】:

    file() 方法以数组形式返回结果,这里可能更适合您的需求。

    $response = file("https://$sitename.webex.com/WBXService/XMLService", true, $request);
    $lastLine = $response[count($response)-1];
    

    https://www.php.net/manual/en/function.file.php

    【讨论】:

    • 不,这只会发布我们正在传递的 XML 数据
    猜你喜欢
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    相关资源
    最近更新 更多