php
  • json
  • jquery
  • jquery-plugins
  • 2013-01-18 73 views 1 likes 
    1

    欲加载域URL在div元素 我有2个问题:谷歌地图AJAX和PHP

    • 如果加载例如“google.com”域以http协议不加载地址图像正确,但加载页面!
    • 如果使用https协议加载谷歌地图给予警告,并且永远不会工作!

    这里是我的代码:

    load.php

    <html> 
    <head> 
        <title></title> 
        <script type="text/javascript" src='jqurey.js'></script> 
    </head> 
    <body> 
        <div></div> 
        <script type="text/javascript"> 
         $(function(){ 
          $('div').load('/x-request.php?url=googleMapAdress'); 
         }); 
        </script> 
    </body> 
    </html> 
    

    X-request.php

    <?php 
        echo file_get_contents('https://' . $_GET['url']); 
    ?> 
    

    注: 不想使用iframe tage &

    我的代码必须有ajax代码!

    什么是解决方案?

    //更多解释

    如何得到任何外部网页,并在完全div元素加载它? 这意味着=> href,src和所有链接都能正常工作。

    回答

    1

    我试过你下面的代码,并按预期工作。

    <html> 
    <head> 
        <title></title> 
        <script type="text/javascript" src='js/jquery.js'></script> 
    </head> 
    <body> 
        <div></div> 
        <script type="text/javascript"> 
         $(function(){ 
          $('div').load('x-request.php?url=maps.google.com'); 
         }); 
        </script> 
    </body> 
    </html>

    PHP代码

    <?php 
        echo file_get_contents('https://' . $_GET['url']); 
    ?>

    其装载谷歌地图没有任何错误。

    编辑

    在下面评论的反应是可能的最佳解决方案。

    您正在寻找的设置是allow_url_fopen

    在php.ini文件中设置allow_url_fopen = On

    你有两种方法可以在不改变php.ini的情况下使用它,其中一个是使用fsockopen,另一个是使用cURL

    卷曲例如

    function get_content($URL) 
    { 
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_URL, $URL); 
        $data = curl_exec($ch); 
        curl_close($ch); 
        return $data; 
    } 
    
    echo get_content('http://example.com');

    快速参考链接是如下用于卷曲。

    http://www.kanersan.com/blog.php?blogId=45

    http://www.tomjepson.co.uk/enabling-curl-in-php-php-ini-wamp-xamp-ubuntu/

    <php 
    $ch = curl_init();// set url 
    curl_setopt($ch, CURLOPT_URL, $_GET['url']); 
    //return the as a string 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch,CURLOPT_BINARYTRANSFER, true); 
    // echo output string 
    $output = curl_exec($ch); 
    $url = $_GET['url']; 
    
    
    $path = 'http://'.$url.'/'; 
    $search = '#url\((?!\s*[\'"]?(?:https?:)?//)\s*([\'"])?#'; 
    $replace = "url($1{$path}"; 
    $output = preg_replace($search, $replace, $output); 
    
    echo $output; 
    
    // close curl resource to free up system resources 
    curl_close($ch);
    +0

    感谢您的答案,但它不是我的目的,googleMapAdress =影射=>从google.map一个地址,如何得到任何外部网页,并在DIV加载元素完全与ajax? – geeking

    +0

    当想要使用https协议获取外部页面时,我看到这个警告:'警告:file_get_contents()[function.file-get-contents]:无法找到包装器“https” - 你忘了启用它时配置PHP?第2行的C:\ xampp \ htdocs \ x-request.php 警告:file_get_contents(https://google.com)[function.file-get-contents]:未能打开流:C:\中的无效参数xampp \ htdocs \ x-request.php第2行' – geeking

    +0

    请看我更新的答案。 –

    相关问题