 |
ถามวิธีสร้าง และ ส่งข้อมูล ของ web service ครับในรูปแบบ php |
|
 |
|
|
 |
 |
|
ลอง Save ไฟล์เป็นแบบ UTF-8 ครับ
|
 |
 |
 |
 |
Date :
2014-03-06 16:23:09 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ได้นะครับ ก็ยังขึ้น error ตัวเดิม เกี่ยวกับ version nusoap ที่ผมใช้อยู่ไหมครับ
ก็อปโค้ดมาด้วยครับ เผื่อใส่อะไรผิด
Code (PHP)
<?
require_once("include/_lib/nusoap.php");
//Create a new soap server
$server = new soap_server();
//Define our namespace
$namespace = "http://localhost:81/intranet/WebServiceServer.php";
$server->wsdl->schemaTargetNamespace = $namespace;
//Configure our WSDL
$server->configureWSDL("HelloWorld");
// Register our method and argument parameters
$varname = array(
'strName' => "xsd:string",
'strEmail' => "xsd:string"
);
$server->register('HelloWorld',$varname, array('return' => 'xsd:string'));
function HelloWorld($strName,$strEmail)
{
return "Hello, World! Khun ($strName , Your email : $strEmail)";
}
// Get our posted data if the service is being consumed
// otherwise leave this data blank.
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
// pass our posted data (or nothing) to the soap service
$server->service($POST_DATA);
exit();
?>
|
ประวัติการแก้ไข 2014-03-07 08:19:23 2014-03-07 08:21:28 2014-03-07 08:22:17
 |
 |
 |
 |
Date :
2014-03-07 08:15:14 |
By :
copsychus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าเป็นฝั่ง .NET การสร้าง/เรียกใช้งาน Webservice จะง่ายกว่าครับ
ตัวอย่างที่ผมมีอยู่ ผมเก็บเอาไว้อ้างอิง ครับ
และผมก็จะเก็บคำถามของคุณเอาไว้อ้างอิงด้วยเช่นกัน Creating Webservice with PHP
.NET Webservice
Code (VB.NET)
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Script.Services
Imports System.ComponentModel
Imports System.Dynamic
Imports System.Linq
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
<System.Web.Script.Services.ScriptService()> _
Public Class WL_WebService
Inherits System.Web.Services.WebService
Private Shared Cars As New List(Of Car)() From {New Car() With {.Make = "Audi", .Model = "A4", .Year = "2004", .Doors = 5, .Colour = "Red", .Price = 2995.0F},
New Car() With {.Make = "Ford", .Model = "Focus", .Year = "2008", .Doors = 4, .Colour = "Blue", .Price = 1220.0F},
New Car() With {.Make = "BMW", .Model = "5 Series", .Year = "2010", .Doors = 2, .Colour = "Green", .Price = 99879.0D},
New Car() With {.Make = "Toyota", .Model = "Vios", .Year = "2012", .Doors = 4, .Colour = "White", .Price = 450.0D}
}
<WebMethod()> _
Public Function GetCarsByDoors(ByVal doors As Integer) As List(Of Car)
'Select * From Production_RDBMS Where Condition?
Dim query = From c In Cars
Where c.Doors = doors
Select c
Return query.ToList() 'ResponseFormat:=ResponseFormat.Json
End Function
<Serializable()> _
Public Class Car
Public Property Make As String
Public Property Model As String
Public Property Year As Integer
Public Property Doors As Integer
Public Property Colour As String
Public Property Price As Double
End Class
End Class
Code (PHP)
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="CallWebserviceSample.aspx.vb"
Inherits="WLWeb.CallWebserviceSample" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="../../Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function getCarsByDoor() {
$.ajax({
type: "POST",
url: "/WL_WebService.asmx/GetCarsByDoors",
data: "{doors: " + $('#<%= ddlDoors.ClientID %>').val() + " }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var cars = response.d;
$('#output').empty();
$.each(cars, function (index, car) {
$('#output').append('<p><strong>' + car.Make + ' ' +
car.Model + '</strong><br /> Year: ' +
car.Year + '<br />Doors: ' +
car.Doors + '<br />Colour: ' +
car.Colour + '<br />Price: £' +
car.Price + '</p>');
});
},
failure: function (msg) {
$('#output').text(msg);
}
});
}
</script>
</head>
<body>
<form id="form2" runat="server">
<div id="output">
</div>
<div>
Number of doors:
<asp:DropDownList ID="ddlDoors" runat="server">
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:DropDownList>
</div>
<input type="button" id="Button2" value="Get Cars By Doors" onclick="getCarsByDoor();" />
</form>
</body>
</html>
|
 |
 |
 |
 |
Date :
2014-03-07 08:42:17 |
By :
love9713 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณสำหรับความช่วยเหลือนะครับ แต่พอดีที่ทำอยู่ ฝั่งผมมันเป็น php และอีกฝั่งก็เป็น php เหมือนกันน่ะครับ
ปัญหาด้านบนแก้ได้แล้วนะคัรบ ขอบคุณมากๆเลยครับ
|
ประวัติการแก้ไข 2014-03-07 09:29:53
 |
 |
 |
 |
Date :
2014-03-07 09:29:27 |
By :
copsychus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนนี้ ตัว server ทำได้แล้วครับ แต่ตัว client ยังไม่ได้รันแล้ว มันก็หมุนๆๆ สักพักก้ขึ้นหน้ากระดาษ เปล่า ทำตามแบบใน เว็บ tutorial เลยครับ

อันนี้โค้ดที่ใช้อยู่ครับ
Code (PHP)
<?
require_once("include/_lib/nusoap.php");
$client = new nusoap_client("http://localhost:81/intranet/WebServiceServer.php",true);
$params = array(
'strName' => "Weerachai Nukitram",
'strEmail' => "[email protected]"
);
$data = $client->call("HelloWorld",$params);
echo $data;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>Test Response</title>
</head>
<body>
</body>
</html>
|
ประวัติการแก้ไข 2014-03-07 11:44:29
 |
 |
 |
 |
Date :
2014-03-07 11:26:11 |
By :
copsychus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ดูๆแล้วน่าจะมีปัญหาที่ตัวนี้น่ะครับ $client = new SoapClient("http://localhost:81/intranet/WebServiceServer.php?wsdl",true);
มันหาไม่เจอ . . . ทั้งที่ๆ url มันก็ถูกนะครับ
|
 |
 |
 |
 |
Date :
2014-03-07 13:18:16 |
By :
copsychus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2014-03-07 15:22:07 |
By :
copsychus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเปลี่ยน localhost เป็น 127.0.0.1 ดูรึยังครับ
|
 |
 |
 |
 |
Date :
2014-03-08 22:44:58 |
By :
news |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|