0
我試圖通過cURL連接到API。當我使用ajax時,它可以工作,但是當我嘗試在Laravel中使用guzzleHTTP或使用簡單的cURL時,我得到「403禁止」。將ajax轉換爲cURL
這是Ajax代碼:
body = '{"qualifier": "registration\/registerUser", "data": {"registrationInfo": {"registrationType": "Lead", "email": "[email protected]", "firstName": "ly", "lastName": "developer test", "telephone": "+41-555-1234567", "countryCode": "zd"}, "marketingInfo": {"adServer": "myAdServer", "adData": "myAdData", "utmCampaign": "myUtmCampaign", "utmContent": "myUtmContent", "utmTerm": "myUtmTerm", "utmSource": "myUtmSource", "utmMedium": "myUtmMedium", "affiliateId": 36293}}, "context": {"apiKey": "dsfcvxmlkddvmdsdmlgkgdsmkgsdflksd", "apiSecret": "lkmlmlk", "locale": "en-US"}}';
$(document).ready(function() {
$("#myclick").click(function() {
$.ajax({
url: 'https://example.com',
type: 'post',
data: body,
success: function (data, textStatus, jqXHR) {
console.log(data);
}
});
});
});
==== UPDATE 這是在PHP代碼在laravel
thank you @pang, plz can you provide a solution for that. this is code in php `
public function sendToNet() {
// Provide the body as a string.
$body = json_encode([
"qualifier" => "v1/registration/registerUser",
"data" => [
"registrationInfo" => [
"registrationType" => "Lead",
"email" => "test040[email protected]",
"firstName" => "firstName",
"lastName" => "vlastName",
"telephone" => "+962-4-123456",
"countryCode" => "SA"],
"marketingInfo" => [
"adServer" => "adServerData",
"adData" => "adData",
"utmCampaign" => "utmCampaign",
"utmContent" => "utmContent",
"utmSource" => "utmSource",
"utmTerm" => "utmTerm",
"utmMedium" => "utmMedium",
"affiliateId" => "45345"
]
],
"context" => [
"locale" => "ar-SA",
"apiKey" => "dmflkdsfmldskdsgksmlkgkslkgdsmlk",
"apiSecret" => "mlkmlkmlk"]
]);
$Client = new Client();
$res = $Client->request("POST", 'https://example.com', [
'json' => $body
])->getBody();
dd($res);
try {
} catch (\GuzzleHttp\Exception\ClientException $e) {
dd($e);
}
}
,這是我得到的結果是: result image
我該如何解決問題?
當你使用'ajax',有自動發送會話cookie,其他-方式不 –
@YuJiaao謝謝。如果你不介意,你可以提供一個解決方案。 – tilmido