我已經設置了一個代理,它允許我從api唯一可訪問的服務器端檢索數據用戶端。我有幾個端點設置,他們都工作得很好,但現在我在一個點,我需要發送數據回api進行預訂。我需要從Ajax請求中檢索數據並通過php將其傳遞給另一個API
- 檢索代理從Ajax請求的數據
- 在代理將數據發送到從API API
- 返回成功/錯誤消息,Ajax請求
我不知道該怎麼辦這個。這裏是我的Ajax請求:
var settings2 = {
"async": true,
"crossDomain": true,
"url": "http://url?method=hello&format=json&entity=" + id,
"dataType": "json",
"data": data,
"method": "POST"
};
var a2 = $.ajax(settings2);
$.when(a2).done(function(d2) {
rp = d2;
console.log(JSON.stringify(rp));
});
和PHP函數和方法我用了,只有需要從API檢索數據的其他終端,它適用於這樣的:
function LocationReserve(data) {
header('Content-Type: application/json');
$url = 'api_url' . $_GET['entity'] ;
$auth = 'Authorization: key';
$ch = curl_init($url);
$options = array(
CURLOPT_HTTPHEADER => array(
$auth
),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION, 1
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// Method A: Say Hello to the API
if(strcasecmp($_GET['method'],'hello') == 0){
$response['code'] = 1;
$response['status'] = $api_response_code[ $response['code'] ]['HTTP Response'];
$response['data'] = wssLocationReserve();
}
// --- Step 4: Deliver Response
// Return Response to browser
deliver_response($_GET['format'], $response);
以此爲這是我得到響應
{"code":1,"status":200,"data":"{\"Message\":\"The requested resource does not support http method 'GET'.\"}"}
我加CURLOPT_POST =>忠於我的選擇,現在的反應是
{"code":1,"status":200,"data":"<html><head><title>411 Invalid Request</title></head><body>Invalid Request: ??</body>\r\n"}
我該如何修改它以接受來自我的ajax調用的數據,將它發送到api_url併發迴響應?
**編輯例如預期的數據通過API:
{
"ReservationDay": "05/15/2015",
"Units": [{
"UnitID": 12345,
"InsuranceID": 123 (or null)
}],
"PaymentInfo": {
"FirstName": "User",
"LastName": "Userson",
"Address1": "2727 N Central Ave",
"Address2": "",
"City": "Phoenix",
"State": "AZ",
"Zip": "85022",
"Phone": "602-2877878",
"Email": "[email protected]",
"CreditCard": "6011000000000000",
"ExpirationMMYY": "1215",
"CSC": "100"
}
}
這聽起來像你正在尋找** POST **而不是** GET **。使用cURL,你需要'curl_setopt($ ch,CURLOPT_POST,1);'。 –
我添加CURLOPT_POST =>真到我的選擇現在的響應是{ 「代碼」:1, 「狀態」:200, 「數據」:」