0

これは非常に簡単な質問ですが、その方法が見つかりません。フロントエンドには、いくつかの編集ボックスまたはinput-textフィールドを含むフォームがあります。そのうちの 1 つで、ユーザーはブログの URL を書き込み/貼り付けます。ユーザーがボックスを終了すると、次のことを行います。

  1. この URL を取得し、 OpenGraph プロトコルに関連するタグが存在する場合は、そのブログから解析/検索/取得します。この種のタグがない場合は、通常titleの関連タグが使用されます。
  2. 次に、タグの値を使用して、現在のフォーム、タイトル、コメント、説明、画像などの他の編集フィールドに入力します

これらのアクションはどのように達成できますか、AJAXでしょうか? jQueryと一緒に?または他の解決策はありますか?今のところ検索しましたが、役に立つと思われるリンクのいくつかを以下に示します。

私は、いくつかの異なるモデル、レイアウト、コントローラー、バックエンド管理、いくつかのフロントエンド機能などを備えた完全に機能するモジュールを持っています。最初にフォーム。

編集

を使用してAjaxリクエストからの回答を使用して他の入力ファイルを埋めるために、2番目のポイントを達成することができましたXMLHttpRequest。誰かが別の解決策や推奨事項を持っている場合に備えて、ここにさまざまなファイルがあります。コードが長くなりすぎるため、すべてのコードを表示していません。

html ファイル:

<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

ポイント1に答えるために、 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 に答える