[英]Get data from URL as jQuery AJAX do in PHP
我想通過發送一些POST數據來獲取URL的數據。 簡而言之,當jQuery AJAX獲取數據時,我希望在PHP中獲得數據。 此代碼類似於PHP。
$.ajax({
url: "http://example.com/",
type: "POST",
data: data,
dataType: 'json',
success: function (result) {
alert(result);
}
});
您需要為此使用curl 。 大衛·沃爾什(Davidwalsh) 在此提供了一個很棒的教程
//extract data from the post
//set POST variables
$url = 'http://domain.com/get-post.php';
$fields = array(
'lname' => urlencode($_POST['last_name']),
'fname' => urlencode($_POST['first_name']),
'title' => urlencode($_POST['title']),
'company' => urlencode($_POST['institution']),
'age' => urlencode($_POST['age']),
'email' => urlencode($_POST['email']),
'phone' => urlencode($_POST['phone'])
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.