【问题标题】:Google map ajax and php谷歌地图ajax和php
【发布时间】:2013-01-01 20:13:42
【问题描述】:

我想在 div 元素中加载一个域 url

我有两个问题:

  • 如果使用 http 协议加载域,例如“google.com”,则无法正确加载地址图像,而是加载页面!
  • 如果使用 https 协议加载谷歌地图会发出警告并且永远不会工作!

这是我的代码:

load.php

<html>
<head>
    <title></title>
    <script type="text/javascript" src='jquery.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 标签,我的代码必须有 ajax 代码!

解决办法是什么?

//更多解释

如何获取任何外部页面并将其完全加载到 div 元素中? 这意味着 => href、src 和所有链接都可以真正工作。

【问题讨论】:

    标签: php json jquery jquery-plugins


    【解决方案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');

    curl 的快速参考链接如下。

    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);

    【讨论】:

    • 感谢您的回答,但这不是我的目的,googleMapAdress =innuendo=> 来自 google.map 的地址,如何获取任何外部页面并使用 ajax 将其完全加载到 div 元素中?
    • 当想要使用 https 协议获取外部页面时,我看到了这个警告:Warning: file_get_contents() [function.file-get-contents]: Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? in C:\xampp\htdocs\x-request.php on line 2 Warning: file_get_contents(https://google.com) [function.file-get-contents]: failed to open stream: Invalid argument in C:\xampp\htdocs\x-request.php on line 2
    • 谢谢,但又犯错了:Warning: fsockopen() [function.fsockopen]: unable to connect to https://google.com:80 (Unable to find the socket transport "https" - did you forget to enable it when you configured PHP?) in C:\xampp\htdocs\x-request.php on line 2 Unable to find the socket transport "https" - did you forget to enable it when you configured PHP? (5)
    • 老兄,我刚刚给了你一个替代方案,你也可以通过在 php ini 中启用 allow_url_fopen 来使用 file_get_contents 来做到这一点……你试过了吗……再次使用 fsockopen 你需要启用 php_fsockopen
    • 你能给出一个加载外部页面的代码示例吗,我试过但写不出来!!
    猜你喜欢
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多