Php 从xml url提取值
我需要从这个Url打印Identificator标记中的值,但是我没有得到任何输出Php 从xml url提取值,php,xml-parsing,Php,Xml Parsing,我需要从这个Url打印Identificator标记中的值,但是我没有得到任何输出 function get_xmlcontent(StdClass $data) { $year = $data->year; $course = $data->clipid; $typeperiod = $data->typeperiod; if ($typeperiod == 's'||$typeperiod == 'a') {
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;
}
}
如何解决此问题?我使用file\u get\u contents()并将xml字符串传递给SimpleXMLElement,而不是自定义的下载文件内容()函数。希望这能为你指明正确的方向。 考虑在两种方法中拆分功能:a)BuffDILL()b) 回答你的评论:这涉及到另一个问题! 您的域是HTTPS域,因此您需要告诉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();$timeout=5;curl_setopt($ch,CURLOPT_url,');curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout)$data=curl_exec($ch);curl_close($data;}我更新了最初的答案,并给出了一个curl用法的例子。如果你喜欢,就接受正确的答案。谢谢你。通过file\u get\u contents处理xml是可以的,但是通过curl会给我带来错误。致命错误:在C:\moodle25\server\moodle\local\ecoclipaluno\somefile.php:217堆栈跟踪:#0 C:\moodle25\server\moodle\local\ecoclipaluno\somefile.php(217):SimpleXMLElement->\uu构造(“”)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 simplexmleElement($xmlContent);
<?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);