Javascript ve PHP'de nasıl HTTP isteği yapabilirim?
Dec 15 2022
JavaScript'te HTTP isteği yapmak için XMLHttpRequest nesnesini veya daha yeni getirme API'sini kullanabilirsiniz. İşte XMLHttpRequest kullanan bir örnek: Getirme API'sinin yalnızca modern tarayıcılarda desteklendiğini unutmayın, bu nedenle bir çoklu doldurma kullanmanız veya eski tarayıcılar için XMLHttpRequest kullanmaya geri dönmeniz gerekebilir.
JavaScript'te HTTP isteği yapmak için XMLHttpRequestnesneyi veya daha yeni fetchAPI'yi kullanabilirsiniz.
İşte kullanılan bir örnek XMLHttpRequest:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://www.example.com/', true);
xhr.onload = function() {
if (this.status == 200) {
var data = JSON.parse(this.response);
console.log(data);
}
};
xhr.send();
fetch('https://www.example.com/')
.then(response => response.json())
.then(data => console.log(data));
API'nin yalnızca modern tarayıcılarda desteklendiğini unutmayın , bu nedenle bir çoklu doldurma kullanmanız veya daha eski tarayıcılar fetchiçin kullanmaya geri dönmeniz gerekebilir .XMLHttpRequest
PHP'de bir HTTP isteği yapmak için yerleşik file_get_contents() işlevini veya daha güçlü cURL kitaplığını kullanabilirsiniz.
file_get_contents() kullanarak:
<?php
// Set the URL of the request
$url = 'http://www.example.com';
// Send the request and store the response
$response = file_get_contents($url);
// Check for errors
if($response === false) {
// Handle error
} else {
// Use the response
}
?>
<?php
// Initialize cURL
$ch = curl_init();
// Set the URL of the request
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com');
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects
// Send the request and store the response
$response = curl_exec($ch);
// Check for errors
if(curl_errno($ch)) {
// Handle error
} else {
// Use the response
}
// Close the cURL handle
curl_close($ch);
?>Nicole Kidman, Michael Keaton ve Val Kilmer'in Batman Olarak Paylaştığı Bu 1 Çekici Özelliğe Bayıldı
Donovan, Şarkılarından 1'ini The Beatles'ın "Lucy in the Sky with Diamonds" şarkısıyla karşılaştırdı
Tom Girardi Dolandırıcılık Suçlamalarından Yargılanma Yetkisinin Belirlenmesi İçin Duruşmaya Katıldı
Charly Reynolds Yakın Zamandaki Vokal Kord Ameliyatını Açıkladı: 'Şarkı Söylemekte Sorun Yaşıyordum'

![Bağlantılı Liste Nedir? [Bölüm 1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































