How to generate search result shortcode for a PHP system in a PHP page?

Asked

Viewed 123 times

0

I have a PHP system that does some registration and data listing. To explain what I would like to implement, I will simulate some codes below:

$conexao = mysqli_connect(localhost, root, 123456, bancoteste);
$sql = mysqli_query($conexao, "select * from tabela");
$tr1 = mysqli_num_rows($sql);

for ($i=0;$i++;$i<$tr1)
{
  echo "Codigo aqui para exibição dos dados 
  ...";
}

In this case, I would like to make this data available on a PHP page of another website, so that it would not be necessary to make the access data of my database available. Or in other words, it is possible to generate a shortcode of my search result to export to an external PHP page?

Example of shortcode I thought:

[www.site.com.br/15/result]

By including this shortcode in a php page, it would load the data automatically.

  • you want your wordpress display data from a database other than the one used in the installation?

  • I edited my question because my explanation was not clear

  • Yes Luis, now understood, you need to create an API and a Plugin for WP for your idea to work.

  • The sites are in different domains?

2 answers

2

If they are all on the same sites or mysql is accessible between the sites

You can just create a function like this:

global.php:

<?php

function exibirDados(query)
{
    //Evita conectar multiplas vezes
    static $conexao;

    //Cache da resposta
    static $cache;

    if (isset($cache[$query])) {
        echo $cache[$query];
        return;
    }

    if (!$conexao) {
        $conexao = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
    }

    if (!$conexao) {
        echo "Erro ao se conectar com o MySQL:" . PHP_EOL;
        echo "numero: " . mysqli_connect_errno() . PHP_EOL;
        echo "erro: " . mysqli_connect_error() . PHP_EOL;
        exit;
    }

    $sql = mysqli_query($conexao, query);
    $tr1 = mysqli_num_rows($sql);
    $resultado .= '';

    for ($i=0;$i++;$i<$tr1)
    {
      $resultado .= '..... formate aqui';
    }

    $cache[$query] = $resultado;

    echo $resultado;

And then in your file you can do something like this:

<?php
require_once 'global.php';
?>

bla bla bla bla <?php exibirDados('SELECT * ....'); ?>
bla bla bla bla <?php exibirDados('SELECT * ....'); ?>
bla bla bla bla <?php exibirDados('SELECT * ....'); ?>
bla bla bla bla <?php exibirDados('SELECT * ....'); ?>

If they’re different sites

Note that when doing this you will have to do more "downloads", which can increase the consumption of the page, not the code itself, but the idea.

Whatever you want, you can use preg_replace_callback with curl and in the domain that has the data make a page as:

  • php result.

    <?php
    
    if (empty($_GET['pagina'])) {
        return '[pagina não definida]';
    }
    
    $pagina = intval($_GET['pagina']);
    
    if ($pagina < 1) {
        return '[pagina invalida]';
    }
    
    $conexao = mysqli_connect(localhost, root, 123456, bancoteste);
    
    $result = mysqli_query($conexao, "select id, coluna1, coluna2 from tabela LIMIT ..., ...");
    
    while ($row = mysqli_fetch_assoc($result)) {
        echo ...;
    }
    
    mysqli_free_result($row);
    

So in the domain that will receive the data can create something like:

  • global.php:

    <?php
    
    function downloadData($url)
    {
        static $dados;
    
        //Cache de dados, para evitar multiplos downloads repetidos
        if ($dados[$url]) {
            return $dados[$url];
        }
    
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
    
        $resposta = curl_exec($ch);
    
        if (!$resposta) {
            $dados[$url] = '[dados inacessives]';
        } else {
            $dados[$url] = $resposta;
        }
    
        curl_close($ch);
    
        $ch = null;
    
        return $resposta;
    }
    
    function buscaDados($entrada)
    {
        $replace = function($match) {
            return downloadData($match[1]);
        };
    
        return preg_replace_callback('#\[(http:\/\/[a-zA-Z0-9\/%\-_]+?)\]#', $replace, $entrada);
    }
    

Then include the global.php in the pages you will use, it should look something like:

<?php

require_once 'global.php';

echo buscaDados('bla bla bla [http://site1/15/resultado.php] bla bla bla [http://site2/14/busca.php]');

Of course the preferable is you maybe use a format like JSON to handle the data well and maybe use an authentication method, but that’s another story.

Important: On the sites that will include you should return only the important and preferred parts that you have control over all sites included, if you may not have to use http://php.net/manual/en/domdocument.loadhtmlfile.php so will only retain the necessary part.

Note: Maybe not the best approaches, I recommend thinking about maybe an API or something in an existing project organization structure, like MVC, if you know how to use it, if you use without understanding that the reason is to organize, or use anyway, will also be a problem.

Sometimes reinventing the wheel is cool but provided it well designed and with a good test time to make sure it will be useful to put into production.

0

You need to create a API in your system PHP and a plugin for Wordpress so it consumes the data of its API.

  • 1

    Was that supposed to be a real answer? To me it seems like a comment, because it’s something very broad and generic.

  • @Victorstafusa did you see how this question was marked? understand my answer now?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.