2016-01-23 71 views
0

我想通過發送一些POST數據來獲取URL的數據。簡單地說,就像jQuery AJAX獲取數據一樣,我想在PHP中使用它。 PHP中的這段代碼的相似之處。從URL獲取數據作爲jQuery AJAX在PHP中做的

$.ajax({ 
    url: "http://example.com/", 
    type: "POST", 
    data: data, 
    dataType: 'json', 
    success: function (result) { 
     alert(result); 
    } 
}); 
+1

的file_get_contents( 'your_target_url');可以從您請求的網址獲取數據。 – ameenulla0007

+0

file_get_contents不適用於後期 - 谷歌捲曲,(捲曲URL請求庫) –

+0

是的,你可以捲曲這 – devpro

回答

1

你需要使用捲曲了點。上有一個很大的教程由davidwalsh here

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