簡體   English   中英

PHP等同於Javascript XMLHttpRequest

[英]PHP equivalent of Javascript XMLHttpRequest

我有一個PHP函數:

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

我正在使用Javascript Ajax調用此函數:

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

現在,而不是使用javascript ajax在php文件中調用saveSnapshot函數,我想從其他一些PHP文件中調用saveSnapshot函數。

我該怎么做呢? 如何撥打電話? 如何傳遞參數?

如果您不想在下面添加外部庫示例,則cURL是一個不錯的選擇: 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);

我更喜歡使用Guzzle之類的庫,因為它使我不必重新創建輪子。

食屍鬼示例: 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));

這里不需要額外的庫...您可以使用file_get_contents()進行POST,而php具有構建url的功能。 我可能會讓它看起來像這樣:

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

基本的cURL示例。 可以在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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM