如何使用php curl将其他网站内容加载到我网站的div中

时间:2013-09-29 18:14:48

标签: javascript php jquery curl

我试过这种方式(根据“jerjer”的选定答案)但未能加载内容..

Load external site's content

render.php

<?php
    $url = 'http://apps.irs.gov/app/withholdingcalculator/index.jsp';
    $htm = file_get_contents($url);
    echo $htm;
?>

common.js

$(document).ready(function(){
    $('#divId').load('render.php');
});

index.html

<html>
<head>
    <title>Home</title>
    <link rel="stylesheet" href="css/style.css" type="text/css">
    <script src="js/jquery.js"></script>
    <script src="js/common.js"></script>
</head> 
<body>
    <div id="divId"></div>
</body>
</html>

有任何帮助吗?提前谢谢......

1 个答案:

答案 0 :(得分:1)

使用此代码。确保安装了卷曲扩展。

 $url = 'http://apps.irs.gov/app/withholdingcalculator/index.jsp';
 $htm = getCurlData($url);
 echo $htm;

function getCurlData($url)
{
     $ch = curl_init($url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
     $contents = curl_exec($ch);
     curl_close($ch);
     return $contents;
}