【问题标题】:Extracting the values from a xml url从 xml url 中提取值
【发布时间】:2014-01-20 10:23:04
【问题描述】:

我需要从这个 Url https://clip.unl.pt/sprs?lg=pt&year=2013&uo=97747&srv=rsu&p=1&tp=s&md=3&rs=8145&it=1030123459 打印 Identificador 标记内的值,但我根本没有得到任何输出。

function get_xmlcontent(StdClass $data)
{
    $year       = $data->year;
    $course     = $data->clipid;
    $typeperiod = $data->typeperiod;    

    if ($typeperiod == 's'||$typeperiod == 'a') {
        $period = $data->period;
    } else if ($typeperiod == 't') {
        $period = $data->trimester;
    }

    //file from CLIP
    $xmlUrl = 'https://clip.unl.pt/sprs?lg=pt&year='.$year.'&uo=97747&srv=rsu&p='.$period.'&tp='.$typeperiod.'&md=3&rs='.$course.'&it=1030123459';

    $xmlStr = download_file_content($xmlUrl);
    $xmlObj = simplexml_load_string($xmlStr);
    foreach ($xmlObj->unidade_curricular->inscritos->aluno as $aluno) {
        echo $result = $aluno->identificador;
    }
}

我该如何解决这个问题?

【问题讨论】:

    标签: php xml-parsing


    【解决方案1】:

    我没有使用自定义的 download_file_content() 函数,而是使用了 file_get_contents() 并将 xml 字符串传递给 SimpleXMLElement。希望这会为您指明正确的方向。 考虑将功能拆分为两种方法:a) buildURL() b) fetchXMLContent()。

    $xmlContent = file_get_contents('https://clip.unl.pt/sprs?lg=pt&year=2013&uo=97747&srv=rsu&p=1&tp=s&md=3&rs=8145&it=1030123459');
    $xmlObj = new SimpleXMLElement($xmlContent);
    
    foreach($xmlObj->unidade_curricular->inscritos->aluno as $aluno){
        $result= $aluno->identificador;
        echo $result;
    }
    

    回答您的评论:这涉及另一个问题! 您的域是 HTTPS 域,因此您需要告诉 cURL 如何处理它。 我创建了一个示例,它解决了所有提到的问题并演示了 cURL 用法。

    <?php
    
    function buildURL($year, $period, $typeperiod, $course)
    {
        return 'https://clip.unl.pt/sprs?lg=pt&year='.$year.'&uo=97747&srv=rsu&p='.$period.'&tp='.$typeperiod.'&md=3&rs='.$course.'&it=1030123459';
    }
    
    function doRequest_with_FileGetContents($url)
    {
       return file_get_contents($url);
    }
    
    function doRequest_with_cURL($url) {
      $ch=curl_init();
      curl_setopt($ch,CURLOPT_URL, $url);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    
      // your domain is a HTTPS domain, so you need to tell curl how to deal with SSL verifications
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    
      $data=curl_exec($ch);
      curl_close($ch);
    
      return $data;
    }
    
    function processXML($xmlContent)
    {
       $xmlObj = new SimpleXMLElement($xmlContent);
    
        foreach($xmlObj->unidade_curricular->inscritos->aluno as $aluno){
           $result= $aluno->identificador;
           echo $result;
        }
    }
    
    // Ok. Lets test..
    
    // some testing values, i guess these will come from a formular
    $year = '2013';
    $period = '1';
    $typeperiod = 's';
    $course = '8145';
    
    // a) build URL
    $url = buildURL($year, $period, $typeperiod, $course);
    
    // b) fetch content
    $content_a = doRequest_with_cURL($url);
    
    $content_b = doRequest_with_FileGetContents($url);
    
    // c) process content (should be the same)
    echo "\nRequest with cURL\n";
    processXML($content_a);
    
    echo "\nRequest with file_get_contents()\n";
    processXML($content_b);
    

    【讨论】:

    • 感谢您的帮助。我试图构建一个 curl 函数来获取我的 url 信息,但是,我似乎无法将它与 fetchxml 函数联系起来。函数 get_url($year,$period,$typeperiod,$course){ $ch=curl_init(); $超时=5; curl_setopt($ch,CURLOPT_URL,'clip.unl.pt/…); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $data=curl_exec($ch); curl_close($ch);返回$数据; }
    • 我更新了我的初始答案,并提供了一个 cURL 使用示例。如果您喜欢它,请接受正确的答案。谢谢
    • 谢谢。通过 file_get_contents 处理 xml 是可以的,但是通过 curl 给我错误。致命错误:在 C:\moodle25\server\moodle\local\ecoclipaluno\somefile.php:217 中未捕获的异常 'Exception' 和消息 'String could not be parsed as XML' 堆栈跟踪:#0 C:\moodle25\server\ moodle\local\ecoclipaluno\somefile.php(217): SimpleXMLElement->__construct('') #1 C:\moodle25\server\moodle\local\ecoclipaluno\somefile.php(235): processXML(false) #2 { main} 在 C:\moodle25\server\moodle\local\ecoclipaluno\somefile.php 第 217 行抛出 $xmlObj= new SimpleXMLElement($xmlContent);
    猜你喜欢
    • 2019-01-07
    • 1970-01-01
    • 2013-02-15
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 2021-03-27
    • 1970-01-01
    相关资源
    最近更新 更多