2017-05-25 38 views
0

我有一個PHP函數:相當於PHP的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); 

我打電話使用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)); 

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

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

+0

你可以去看一下PHP'cURL' - http://codular.com/curl-with-php – Diego

+0

有幾種方法,但我更喜歡捲曲作爲最簡單的方法。 – richbai90

+0

@Diego你可以發表一些關於此的代碼嗎? – user5155835

回答

1

捲曲發現是一個不錯的選擇,如果你不想在下面添加外部庫的例子: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); 

我更喜歡使用像狂飲圖書館,因爲它允許我不必重新創建輪子。

狂飲例子: 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

感謝您的捲曲代碼。另外,在'reqType'=> $ saveNewSnapshot中,$不應該在那裏,它應該只是'reqType'=> saveNewSnapshot。由於saveNewSnaphot是函數名稱 – user5155835

+0

啊,我沒有意識到這是一個函數!希望這回答你的問題!如果是這樣,我會很感激,如果你可以接受這個作爲你的答案:) –

+0

你可以進行更改,使$ saveNewSnapshot保存新快照 – user5155835

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