在github上托管了一个C程序文件,我试图从原始文件URL使用php获取其内容。 这是我目前使用的代码。
<?php
$x = file_get_contents("https://raw.githubusercontent.com/HiteshGarg/codingeek/master/Data-Structure/Queue/PriorityQueue.c");
echo $x;
?>
这可以获取文件,但它会删除<stdio.h> and <stdlib.h>
,并在尝试渲染themby附加其结束标记时将其视为HTML标记,如< - p>
#include<stdio.h>
#include<stdlib.h>
//Other CODE in FILE
</stdlib.h></stdio.h>
我也尝试过使用curl但没有运气。
$curl = curl_init('https://raw.githubusercontent.com/HiteshGarg/codingeek/master/Data-Structure/Queue/PriorityQueue.c');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$x = curl_exec($curl);
echo $x;
有没有办法证明这一点?
编辑 - 真实的情况是我使用的是Wordpress,我必须将这些代码嵌入到代码荧光笔中。为此,我添加了一个短代码,但这会在网页上显示的代码中显示其他HTML代码。因此,我不必在真实场景中使用echo或print,而是必须获得纯文本并将其合并到现有帖子中。
答案 0 :(得分:1)
在您披露了这个场景后,解决方案将是转义HTML序列,以便您的wordpress不再自动关闭标签。
<?php
$x = file_get_contents("https://raw.githubusercontent.com/HiteshGarg/codingeek/master/Data-Structure/Queue/PriorityQueue.c");
echo htmlentities($x);
?>
(编辑:从之前删除旧答案)