Write to a file from another PHP site

Asked

Viewed 41 times

-1

Hello, I have two sites, I need to send data from one to the other with php

Site 1:

<!DOCTYPE html>
<html>
<head>
    <title>Site 1</title>
</head>
<body>
<form method="POST">
    <label>Texto</label>
    <input type="text" name="texto">
    <input class="btn btn-sucess" type='submit' name='submit' value='Gravar'>
</form>
</body>
</html>

Site 2: (It will receive the form data and record in the file itself but do not know how to make the form go to the other site)

<?php
$pegou = $_POST['texto'];
$fp = fopen("index.php", "a");

$txt = "\n";
fwrite($fp, $txt);
$txt = " ".$pegou."\n";
fwrite($fp, $txt);


fclose($fp); 
  • <form method="POST" action="index.php">I think you want to put the form action, that’s it. Or you want it in ajax?

  • Yes, but the form data is on site 1, have to go to site 2, the action serves for other sites as well?

  • There is a rule of security for each site, what you want to do is basically Restful, use an API, one site and publish in another, first you need to have access to both properties of the sites, or have a CORS access, and to do so, only using ajax.

2 answers

0

There are "n" ways to do this, let me give you an example of 2.

  1. You redirect your form to send to the second site, the fields should have the same ID’s to the site that will receive interpret them and complete the action. Example:

    <form action="http://www.siteexemplo.com.br/pagina.php" method="POST">
    
  2. You send the form to yourself, receive the data and send it via php/Curl to another site. This way, if the site returns some confirmation value you can treat/validate, especially if it is an API. It looks more or less like sending:

    $dadosParaEnviar = http_build_query($arrayDadosRecebidos);
    $url = "http://www.siteexemplo.com.br/pagina.php";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $dadosParaEnviar);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        throw new \Exception('cURL error: '. curl_error($ch));
    }
    $resultHeader = curl_getinfo($ch);
    curl_close($ch);
    
  • 1

    Thank you very much, I was able to adapt your codes

0


As @Leonardogetulio said, n ways to do this, I’ll give an example of how to do this through ajax:

Your HTML on the site a:

<!DOCTYPE html>
<html>
<head>
    <title>Site 1</title>
</head>
<body>
<form method="POST">
    <label>Texto</label>
    <input type="text" id="texto" name="texto">
    <button class="btn btn-sucess" id="send">Gravar</button>
</form>
</body>
</html>

The javascript:

<script>

 document.addEventListener("DOMContentLoaded", function(event) {
  //depois que o dom for lido


    function processRequest() {

        //AQUI VOCÊ RECEBE OS DADOS DO SITE DOIS
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                //sucesso, você obterá retorno de um JSON com dados 
                console.log(xhr)
             }else {
               //erro, vc obterá retorno de um JSON com erros
               console.log(xhr)
            }
        }
    }
   var button = document.getElementById('send'),
       input_text = document.querySelector('#texto'),
       actionSend = function() {

               //ENVIA PARA O SITE DOIS
                var url = 'http://www.sitedois.com.br/index.php',
                    dados = {
                       texto: input_text.value;
                    };
                 var xhr = new XMLHttpRequest();

                 if (xhr.withCredentials !== undefined) {
                   /* AQUI VOCÊ PROVAVELMENTE VAI PRECISAR 
                      VALIDAR AS CREDENCIAIS ENVIANDO NO HEADER 
                      O QUE O SITE PEDE PARA PODER LIBERAR O DADO */
                  //xhr.setRequestHeader("PARAMETROS","VALORES");
                 }
                var randomNum = Math.round(Math.random() * 10000);

                 xhr.open("POST", url + '?rnd='+randomNum, false);
                 xhr.send(JSON.stringify(dados));
                 xhr.addEventListener("readystatechange", processRequest, false);
         }
          xhr.addEventListener("readystatechange", processRequest, false);
          button.addEventListener("click", actionSend, false); 


});

</script>

Browser other questions tagged

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