PHP相当于Javascript XMLHttpRequest

时间:2017-05-25 15:01:51

标签: javascript php ajax

我有一个PHP函数:

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调用来调用此函数:

fetchA() {
  fetch(aURL)
    .then(response => response.json())
    .then(json => {
      this.setState({ a: json})
    })
}

fetchB() {
  fetch(bURL)
    .then(response => response.json())
    .then(json => {
      this.setState({ b: json})
    })
}

现在不是使用javascript ajax在php文件中调用saveSnapshot函数,而是想从其他一些PHP文件中调用saveSnapshot函数。

我该怎么做?我该怎么打电话?如何传递参数?

3 个答案:

答案 0 :(得分:1)

如果您不想在下面添加外部库示例,则

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这样的库,因为它允许我不必重新创建轮子。

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

答案 1 :(得分:0)

此处不需要额外的库...您可以使用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));

答案 2 :(得分:0)

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