PHP выполнить через curl_exe c иметь Ajax запрос, который не работает - PullRequest
0 голосов
/ 16 февраля 2020

Я сделал пример своей проблемы. У меня 4 файла

  1. index. php
  2. RunVia Ajax. php
  3. SendRequest. php
  4. AjaxFile . php

index. php Файл

<!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    </head>
    <body>

        <button id="OK">Test Ajax</button>

       <script> 
    $("#OK").click(function(){
        $.ajax({
            type: "POST",
            url: 'RunViaAjax.php',
            data: {data: "someData"}, // serializes the form's elements.
            success: function(data){
                console.log(data);
            }
        });
    }); 
        </script>
    </body>
</html>

RunVia Ajax. php Файл

<?php

    $curl = curl_init();
    // Set some options - we are passing in a useragent too here
    curl_setopt_array($curl, [
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => 'http://localhost/tester/SendRequest.php',
        CURLOPT_USERAGENT => 'Codular Sample cURL Request'
    ]);
    // Send the request & save response to $resp
    $resp = curl_exec($curl);
    echo $resp;
    // Close request to clear up some resources
    curl_close($curl);

?>

SendRequest. php Файл

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

</head>
<body>
    <p id="result"></p>
    <script> 
    $.ajax({
      type: "POST",
      url: 'AjaxFile.php',
      data: {data: "someData"}, // serializes the form's elements.
      success: function(data){
        $('#result').html(data);
      }
    });

    </script>
</body>
</html>

AjaxFile. php Файл

<?php 

    file_put_contents("OK-Report.php", "");
    echo "OK REPORT";

?>

Теперь, запустив index. php, я получаю исходный код SendRequest. php. Мне нужно запустить SendRequest. php (SendRequest. php - динамически создаваемый файл).

1 Ответ

0 голосов
/ 16 февраля 2020

Надеюсь, это поможет вам.

File1.php

$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://localhost/test/MyFile.php',
    CURLOPT_USERAGENT => 'Codular Sample cURL Request'
]);
// Send the request & save response to $resp
$resp = curl_exec($curl);
print_r($resp);
// Close request to clear up some resources
curl_close($curl);

MyFile.php

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

</head>
<body>

    <p id="result"></p>
   <script> 
    $.ajax({
           type: "POST",
           url: 'AjaxFile.php',
           data: {data: "someData"}, // serializes the form's elements.
           success: function(data)
           {
            $('#result').html(data);
           }
         });

    </script>
</body>
</html>

AjaxFile.php

<?php echo 'Ajax request recieved' ?>
...