0

这应该是一个非常简单的问题,但找不到如何去做。在前端,我有一个带有一些编辑框或input-text字段的表单。在其中一个中,用户将编写/粘贴博客网址。当用户退出我想要的框时:

  1. 获取此 url 并从该博客解析/搜索/获取与OpenGraph 协议相关的标签(如果存在)。如果没有此类标签,title则将使用通常和相关的标签。
  2. 然后使用标签的值来填充当前表单、标题、评论、描述、图像等上的其他编辑字段

这些动作如何才能完成,也许是AJAX ?jQuery一起使用?还是其他解决方案?现在我已经搜索过了,这里有一些我认为可能有用的链接:

我有一个完整的工作模块,有几个不同的模型、布局、控制器、后端管理、几个前端功能等。但在这种情况下,我什至不知道从哪里开始,要修改哪些文件以及要添加什么首先形成。

编辑

设法完成第二点,使用来自 Ajax 请求的答案填充其他输入文件XMLHttpRequest。这是不同的文件,以防万一有人有不同的解决方案或一些建议。没有显示所有代码,因为它太长了。

.phtml 文件:

<div id="blog_link_block">
    <input class="input-text required-entry" onchange="showHint(this.value)"
            name="blog_link" id="blog_link_field" type="text"  style="width: 210px;"
            value="" />
</div>
<div>
    <label for="title_field"><?php echo $this->__('Title'); ?>
        <span class="required">*</span>
    </label><br />
    <input class="input-text required-entry" name="title" id="title_field" type="text"
           style="width: 450px;" value="" />
</div>

<script type="text/javascript">
    function showHint(str)
    {
        <?php $block = Mage::getBlockSingleton('blogtest/product_view');
        $temp = $block->getUrl('blogtest/blogtagsajax/index');?>

        if (str.length==0)
        { 
            document.getElementById("title_field").innerHTML = "";
            return;
        }
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function()
        {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("title_field").value = xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","<?php echo $temp ?>?q="+str,true);
    xmlhttp.send();
}
</script>

布局文件:

<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
    <blogtest_blogtagsajax_index>
        <reference name="root">
            <remove name="root"/>
        </reference>
        <block type="blogtest/product_ajax" name="product.ajax" output="toHtml" />
    </blogtest_blogtagsajax_index>
</layout>

控制器:

<?php
class Dts_Blogtest_BlogtagsajaxController extends Mage_Core_Controller_Front_Action {

    public function indexAction() {
        $this->loadLayout();
        $this->renderLayout();
    }
}

php 文件来处理请求:

<?php
class Dts_Blogtest_Block_Product_Ajax extends Mage_Core_Block_Template {

    public function __construct(){
        echo self::myFunc();
    }

    public function myFunc() {
        $a[]="Anna";
        $a[]="Brittany";
        $a[]="Cinderella";
        $a[]="Diana";

        //get the q parameter from URL
        $q=$_GET["q"];

        //lookup all hints from array if length of q>0
        if (strlen($q) > 0)
        {
            $hint="";
            for($i=0; $i<count($a); $i++){
                if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))){
                    if ($hint==""){
                        $hint=$a[$i];
                    }
                    else{
                        $hint=$hint." , ".$a[$i];
                    }
                }
            }
        }

        if ($hint == ""){
            $response="no suggestion";
        }
        else{
            $response=$hint;
        }

        //output the response
        return $response;
    }
}
4

1 回答 1

0

要回答第一点,尝试使用OpenGraphNode库但遇到一些错误

然后我在 SO 上找到了这个问题/答案,所有问题都消失了。已接受答案的建议功能与og:标签完美配合。这是我的修改版本:

function file_get_contents_curl($url)
{
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
}

public function myFunc() {
    // get the q parameter from URL
    $q = $_GET["q"];

    $html = self::file_get_contents_curl($q);

    //parsing begins here:
    $doc = new DOMDocument();
    @$doc->loadHTML($html);
    $nodes = $doc->getElementsByTagName('title');
    $title = $nodes->item(0)->nodeValue;
    $metas = $doc->getElementsByTagName('meta');

    for ($i = 0; $i < $metas->length; $i++){
        $meta = $metas->item($i);
        if($meta->getAttribute('name') == 'og:title')
            $title = $meta->getAttribute('content');
        if($meta->getAttribute('name') == 'og:description')
            $description = $meta->getAttribute('content');
        if($meta->getAttribute('name') == 'keywords')
            $keywords = $meta->getAttribute('content');
    }

    $title = trim($title);
    if ($title  == ""){
        $response = "no title";
    }
    else{
        $response = $title;
    }
    //output the response
    return $response;
}
于 2012-10-19T10:44:23.763 回答