如何通过php请求返回html中的http内容


How to return http content in html by php request?

我有一个包含一些内容(一些广告)的http地址。比方说

http://servername/submit/rest/getHtmlAdvertisements/

我想在html页面上返回这些内容。比方说在中

<div></div>

首先我必须连接到这个http://,然后我必须在div中显示内容(通过jquery?)。我该怎么做?

您可以通过jQuery使用load()来完成:

$( "div" ).load( "http://servername/submit/rest/getHtmlAdvertisements/" );

或通过PHP使用file_get_contents():

<?php
$someData = file_get_contents('http://servername/submit/rest/getHtmlAdvertisements/');
echo "<div>".$someData."</div>";

有几种方法,可以使用PHP curl来获取网站内容,如:

<?php
    function curl_post($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $curl_response = curl_exec($ch);
        curl_close($ch);
        return $curl_response;
    }
    $content = curl_post('http://servername/submit/rest/getHtmlAdvertisements/');
?>

现在,您可以将<div></div>代码编辑为以下内容来显示内容:

<div><?php echo $content; ?></div>

或者使用file_get_contents,如:

<?php
    $content = file_get_contents('http://servername/submit/rest/getHtmlAdvertisements/');
    echo $content;
?>