Search code examples
php-curl

PHP execute via curl_exec have Ajax request which is not working


I made a example of my problem. I have 4 files

  1. index.php
  2. RunViaAjax.php
  3. SendRequest.php
  4. AjaxFile.php

index.php File

<!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>

RunViaAjax.php File

<?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 File

<!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 File

<?php 

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

?>

Now by running index.php, am getting source code of SendRequest.php. What i need is to run SendRequest.php (SendRequest.php is dynamically created file).


Solution

  • Hope this help you.

    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' ?>