2

I have a PHP function:

function saveSnapshot() {
    header("Content-Type: application/JSON: charset=UTF-8");
    global $CFG;
    $resString = "{\"Success\": \"True\"}";

    $snapshotName = getArgument("snapshotName");
    $user = getArgument("userId");
    $ttd = getArgument("ttData");
    $fed = getArgument("feData");
    $ttData = json_decode($ttd, true);  
    $feData = json_decode($fed, true);  

And I'm calling this function using Javascript Ajax call:

xhttp.open("POST", "myfile.php", true); // asynchronous
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xhttp.send("reqType=saveNewSnapshot&newSnapshotName=" + newSnapshotName + "&currentSnapshotName=" + currentSnapshotName +
                    "&configId=" + currentConfigId + "&ttData=" + JSON.stringify(timeTable) +
                    "&feData=" + JSON.stringify(fixedEntry));

Now instead of calling the saveSnapshot function in php file using javascript ajax, I want to call the saveSnapshot function from some other PHP file.

How do I do this? How do I make the call? How do I pass the parameters?

10
  • You can take a look to PHP cURL - codular.com/curl-with-php Commented May 25, 2017 at 15:05
  • There are a few ways, but I prefer cURL as the easiest method. Commented May 25, 2017 at 15:05
  • @Diego can you post some code regarding this? Commented May 25, 2017 at 15:07
  • @richbai90 can you post some code regarding this? Commented May 25, 2017 at 15:07
  • 1
    Honestly, the easiest way to do this would be with a library like Guzzle. cURL is a PITA Commented May 25, 2017 at 15:07

3 Answers 3

2

cURL is a good option if you don't want to add an external library example below: http://php.net/manual/en/ref.curl.php

// Initialize curl object
$ch = curl_init();

// Create post data
$data = array(
    'reqType' => saveNewSnapshot,
    'newSnapshotName' => $newSnapshotName,
    'currentSnapshotName' => $currentSnapshotName,
    'configId' => $currentConfigId,
    'ttData' => $timeTable,
    'feData' => $fixedEntry
);

// Set curl options
curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => 1, // Return information from server
    CURLOPT_URL => 'myfile.php',
    CURLOPT_POST => 1, // Normal HTTP post 
    CURLOPT_POSTFIELDS => $data
));

// Execute curl and return result to $response
$response = curl_exec($ch);
// Close request
curl_close($ch);

I prefer to use a library like Guzzle, because it allows me to not have to recreate the wheel.

Guzzle Example: http://docs.guzzlephp.org/en/latest/overview.html

use GuzzleHttp\Client;

$client = new Client([
    'base_uri' => '/',
    'timeout'  => 2.0,
]);

// Create post data
$data = array(
    'reqType' => saveNewSnapshot,
    'newSnapshotName' => $newSnapshotName,
    'currentSnapshotName' => $currentSnapshotName,
    'configId' => $currentConfigId,
    'ttData' => $timeTable,
    'feData' => $fixedEntry
);

$response = $client->post('myfile.php', array($data));
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the curl code. Also, in 'reqType' => $saveNewSnapshot, $ should not be there, it should only be 'reqType' => saveNewSnapshot. Since saveNewSnaphot is the function name
ah I didn't realize that was a function! Hopefully this answers your question! If so I'd appreciate it if you could accept this as your answer :)
Can you make the changes, make $saveNewSnapshot to saveNewSnapshot
Sure. I added that in there. Is saveNewSnapshot a method that you are running?
Yes, saveNewSnapshot is a method
|
0

No need for extra libraries here... you can use file_get_contents() to POST, and php has functions to build urls. I would probably make it look something like this:

<?php

$query = http_build_query(
    array(
        'reqType' => 'data',
        'newSnapshotName' => 'example',
        'currentSnapshotName' => '1',
        'configId' => '2',
        'ttData' => '4',
        'feData' => '5'
    )
);

$options = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded'
    )
);

file_get_contents('http://server.com/myfile.php?' . $query, false, stream_context_create($options));

Comments

0

Basic cURL Example. More options can be found at http://php.net/manual/en/function.curl-setopt.php

<?php

$curl = curl_init();
// set the options we want
curl_setopt_array($curl, array(
    // Return the response from the server
    CURLOPT_RETURNTRANSFER => 1,
    // The URL we wish to post to
    CURLOPT_URL => 'myfile.php'
    // Add our headers
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/JSON: charset=UTF-8'
    )
    // Post
    CURLOPT_POST => 1,
    // Set post fields
    CURLOPT_POSTFIELDS => array(
        reqType => 'saveNewSnapshot',
        newSnapshotName= => 'newSnapshotName'
        // ...
    )
));

// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.