2013-11-02 48 views
-3

我正在學習編程。我在我的XAMPP中有一個php文件,這個文件用於將數據插入到我的數據庫中。如何通過POST方法調用Web服務

我想用post方法調用php文件並執行sql字符串。

我的PHP文件:

<?php 
$DB_HostName = "localhost"; // ten host 
$DB_Name = "QM_TEST";   
$DB_User = "root";   
$DB_Pass = "";    
$DB_Table = "Customer";    

$name = $_GET[@"name"]; 
$address = $_GET[@"address"]; 

$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error()); 
mysql_select_db($DB_Name,$con) or die(mysql_error()); 
$sql = "insert into $DB_Table (name, address) values('$name','$address');"; 
$res = mysql_query($sql,$con) or die(mysql_error()); 

mysql_close($con); 
if ($res) { 
    echo "success"; 
}else{ 
    echo "faild"; 
}// end else 
?> 
+1

這與OSX或Cocoa有什麼關係? – dbf

+0

它與OSx無關,它不是很清楚你有什麼問題。 –

回答

0

你可以用[stream_context_create()][1]做,如果你不想使用捲曲。

下面是一個例子:

$url = 'http://server.com/path'; 
$data = array('key1' => 'value1', 'key2' => 'value2'); 

// use key 'http' even if you send the request to https://... 
$options = array(
    'http' => array(
     'header' => "Content-type: application/x-www-form-urlencoded\r\n", 
     'method' => 'POST', 
     'content' => http_build_query($data), 
    ), 
); 
$context = stream_context_create($options); 
$result = file_get_contents($url, false, $context); 

var_dump($result); 

有一個類似的問題here

+0

對不起!我想在Xcode中編碼:( – user2948373