2017-05-25 3 views
0

Ich habe eine PHP-Funktion:PHP-Äquivalent von Javascript XMLHttpRequest

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); 

Und ich rufe diese Funktion verwenden Javascript Ajax-Aufruf:

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)); 

Anstatt nun den Aufruf der saveSnapshot Funktion in PHP-Datei Mit JavaScript Ajax möchte ich die saveSnapshot-Funktion aus einer anderen PHP-Datei aufrufen.

Wie mache ich das? Wie mache ich den Anruf? Wie gebe ich die Parameter weiter?

+0

Sie einen Blick auf PHP 'cURL' nehmen kann - http://codular.com/curl-with-php – Diego

+0

Es gibt ein paar Möglichkeiten gibt, aber ich ziehe cURL als die einfachste Methode. – richbai90

+0

@Die können Sie etwas Code diesbezüglich schreiben? – user5155835

Antwort

1

cURL ist eine gute Option gefunden werden, wenn Sie nicht wollen, um eine externe Bibliothek Beispiel unten hinzuzufügen: 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); 

ich es vorziehen, eine Bibliothek wie Guzzle zu verwenden, weil es mir erlaubt, das Rad nicht neu zu erstellen.

Guzzle Beispiel: 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)); 
+0

Danke für den Curl-Code. Außerdem sollte in 'reqType' => $ saveNewSnapshot $ nicht vorhanden sein, es sollte nur 'reqType' => saveNewSnapshot sein. Da saveNewSnaphot ist der Funktionsname – user5155835

+0

ah ich habe nicht erkannt, dass das eine Funktion war! Hoffentlich beantwortet das deine Frage! Wenn ja, würde ich es begrüßen, wenn Sie dies als Ihre Antwort akzeptieren könnten :) –

+0

Können Sie die Änderungen vornehmen, machen Sie $ saveNewSnapshot zu speichernNewSnapshot – user5155835

0

Keine Notwendigkeit für zusätzliche Bibliotheken hier ... Sie können file_get_contents() zu POST verwenden, und php hat Funktionen zum Erstellen von URLs. Ich würde es wahrscheinlich in etwa so aussehen lassen:

<?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)); 
0

Grundlegende cURL Beispiel. Weitere Optionen können bei 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);