ساخت soap API در PHP

ساخت soap API در PHP

تاریخ انتشار: 26 بهمن 1392

تعداد بازدید: 0 نفر

در این مطلب قصد داریم روشی را آموزش دهیم تا بتوانید به وسیله ی آن مقدار زیادی محتوا را در سایت خود با استفاده از تکنولوژی API بگنجانید. این روش برمیگردد به یکی از زیرشاخه های اصلی در زمینه وب سایت و NET. به نام "توسعه".
وقتی با گوگل کار کنید، مجبورید از تکنولوژی Soap , WSDL استفاده کنید. دو تکنولوژی ساده در روش API برای کسانی که قصد ساخت آن را در سایت خود دارند. ما از قبل از یک کلاس SoapClient عالی در سایتمان استفاده کردیم، با این حال یک Soapserver دیگر هم هست که با WSDL کار میکند که در این آموزش یاد میگیرید تا این دو تکنولوژی ارائه شده توسط php را با هم ترکیب کنید تا بتوانید محتوایتان را با دنیا به اشتراک بگذارید.
مزیت استفاده از WSDL این است که میتوانید به جای نوشتن چند هزار خط کد PHP و قرار دادن محتوا بین این کدها، به طور شفاف و مقطعی API را در یک محیط سورس باز xml استاندارد سازی کنید. WSDL پایه های کدنویسی را برای ما فراهم میکند، اما در عین حال این توانایی را دارد که اسناد لازم را خود به خود ایجاد کند، که این مزیتی عالی برای کسانی است که علاقه ای به نوشتن اسناد ندارند. این حقیقت که WSDL کدهای سرور را برای مشتریان فراهم میکند مزیتی دیگر است که باعث میشود فقط احتیاج به تغییر یک فایل در API داشته باشید تا بتوانید محتوای سایت را آپدیت کنید.

طرز کار WSDL

اولین قدم در ایجاد یک API این است که در ابتدا تصمیم بگیرید که به چه نتیجه ای میخواهید برسید. این نتیجه معمولاً با کشیدن چند نمودار که برای تفسیر یک لیست از ورودی ها و خروجی های موردنیاز در یک پردازش به کار میرود، به دست می آید. یک مثال ساده برایتان میزنم : فرض کنیم که میخواهیم یک پازل را در برنامه تعریف کنیم. برای اینکار API باید یک تابع ( )getPuzzel را تعریف کند که یک درخواست را به سرور اصلی ارسال کند، یک پازل را تصادفی انتخاب و آن را برگرداند. برای سخت تر کردن پازل، این انتخاب را به مردم میدهید تا بتوانند از بین سه درجه 1 و 2 و 3 میزان سختی بازی را تعیین کنند.
حال به این نکته توجه کنید: وقتی با WSDL کار میکنید، اولین کدی که مینویسید همیشه سخت ترین است. اولین کد اسکریپت WSDL شما مانند نوشتن 40 خط xml است و فهمیدن آن به این راحتی که شما فکر میکنید نیست. اینکار را شاید بتوانید با راه اندازی کدPHP مقایسه کنید، با این تفاوت که اول باید کد WSDL را بنویسید. برای اینکه راحتتر بتوانید این موضوع را بفهمید، در زیر کد WSDL را به دو قسمت تقسیم کردم:
کل کد را در یک فایل html به نام dotnet1.wsdl ذخیره کنید. در ادامه بخش اول این کد را مشاهده میکنید:
</span></span></p> <p dir="ltr" style="margin-top: 0px; margin-bottom: 20px; padding: 14px 25px 0px 0px; border: 0px; font-family: inherit; font-size: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 30.600000381469727px; vertical-align: baseline; margin-right: 0px;"><small><span style="font-family:tahoma,geneva,sans-serif;"><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-size: 18px; font-weight: bold; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;?xml</span><span style="color: rgb(129, 194, 103); font-size: 18px; line-height: 27.428579330444336px;">&nbsp;</span><span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-size: 18px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(41, 174, 243);">version</span><span style="color: rgb(129, 194, 103); font-size: 18px; line-height: 27.428579330444336px;">=</span><span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-size: 18px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36);">&quot;1.0&quot;</span><span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-size: 18px; font-weight: bold; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(136, 136, 136);">?&gt;</span><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-size: 18px; font-weight: bold; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(136, 136, 136); transition: none !important; -webkit-transition: none !important;">&lt;definitions</span><span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-size: 18px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(41, 174, 243); transition: none !important; -webkit-transition: none !important;">name</span><span style="color: rgb(129, 194, 103); font-size: 18px; line-height: 27.428579330444336px;">=</span><span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-size: 18px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot;NetPuzzle&quot;</span><span style="color: rgb(129, 194, 103); font-size: 18px; line-height: 27.428579330444336px;">&nbsp;</span><span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-size: 18px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(41, 174, 243); transition: none !important; -webkit-transition: none !important;">targetNamespace</span><span style="color: rgb(129, 194, 103); font-size: 18px; line-height: 27.428579330444336px;">=</span><span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-size: 18px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot;urn:NetPuzzle&quot;</span><span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-size: 18px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(41, 174, 243); transition: none !important; -webkit-transition: none !important;">xmlns:typens</span><span style="color: rgb(129, 194, 103); font-size: 18px; line-height: 27.428579330444336px;">=</span><span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-size: 18px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot;urn:NetPuzzle&quot;</span></span></small><br /> <span class="re0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(41, 174, 243); transition: none !important; -webkit-transition: none !important;">xmlns:xsd</span><span style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; color: rgb(129, 194, 103); line-height: 27.428579330444336px;">=</span><span class="st0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot;http://www.w3.org/2001/XMLSchema&quot;</span><span class="re0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(41, 174, 243); transition: none !important; -webkit-transition: none !important;">xmlns:soap</span><span style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; color: rgb(129, 194, 103); line-height: 27.428579330444336px;">=</span><span class="st0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot;http://schemas.xmlsoap.org/wsdl/soap/&quot;</span><br /> <span class="re0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(41, 174, 243); transition: none !important; -webkit-transition: none !important;">xmlns:soapenc</span><span style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; color: rgb(129, 194, 103); line-height: 27.428579330444336px;">=</span><span class="st0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot;http://schemas.xmlsoap.org/soap/encoding/&quot;</span><span class="re0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(41, 174, 243); transition: none !important; -webkit-transition: none !important;">xmlns:wsdl</span><span style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; color: rgb(129, 194, 103); line-height: 27.428579330444336px;">=</span><span class="st0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot;http://schemas.xmlsoap.org/wsdl/&quot;</span><br /> <span class="st0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot;xmlns=&quot;</span><span style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; color: rgb(129, 194, 103); line-height: 27.428579330444336px;">http://schemas.xmlsoap.org/wsdl/</span><span class="st0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot;&gt;&lt;message name=&quot;</span><span style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; color: rgb(129, 194, 103); line-height: 27.428579330444336px;">getPuzzle</span><span class="st0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot;&gt;&lt;part name=&quot;</span><span style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; color: rgb(129, 194, 103); line-height: 27.428579330444336px;">difficulty</span><span class="st0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot; type=&quot;</span><span style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; color: rgb(129, 194, 103); line-height: 27.428579330444336px;">xsd:int</span><span class="st0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot; /&gt;&lt;/message&gt;&lt;message name=&quot;</span><span style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; color: rgb(129, 194, 103); line-height: 27.428579330444336px;">getPuzzleResponse</span><span class="st0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot;&gt;<br /> &lt;part name=&quot;</span><span style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; color: rgb(129, 194, 103); line-height: 27.428579330444336px;">return</span><span class="st0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot; type=&quot;</span><span style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; color: rgb(129, 194, 103); line-height: 27.428579330444336px;">string</span><span class="st0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot; /&gt;&lt;/message&gt;&lt;portType name=&quot;</span><span style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; color: rgb(129, 194, 103); line-height: 27.428579330444336px;">NetPuzzlePort</span><span class="st0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot;&gt;&lt;operation name=&quot;</span><span style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; color: rgb(129, 194, 103); line-height: 27.428579330444336px;">getPuzzle</span><span class="st0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot;&gt;&lt;input message=&quot;</span><span style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; color: rgb(129, 194, 103); line-height: 27.428579330444336px;">typens:getPuzzle</span><span class="st0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot; /&gt;&lt;output message=&quot;</span><span style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; color: rgb(129, 194, 103); line-height: 27.428579330444336px;">typens:getPuzzleResponse</span><span class="st0" style="font-family: tahoma, geneva, sans-serif; font-style: inherit; font-variant: inherit; font-weight: inherit; margin: 0px; padding: 0px; border: 0px; line-height: 27.428579330444336px; vertical-align: baseline; color: rgb(229, 179, 36); transition: none !important; -webkit-transition: none !important;">&quot; /&gt;&lt;/operation&gt;&lt;/portType&gt;</span></p> <p style="text-align: justify;">&nbsp;</p> <p style="margin: 0px 0px 20px; padding: 0px 25px 0px 0px; border: 0px; font-family: inherit; font-size: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 30.600000381469727px; vertical-align: baseline; text-align: justify;"><span style="font-size:16px;">
دو خط ابتدایی برا ی معرفی کد استاندارد xml هستند، گرچه اگر بخواهید از تعدادی کد xml استفاده کنید احتیاج دارید تا ورژن آن را هم ذکر کنید. دوخط بعدی این کد بخش message و operation را در کد معرفی میکند که اولی پیغام مناسب با برنامه را نشان میدهد و دومی عملکردی که مطابق با پیغام دریافت شده است را اجرا میکند. در اول کار به نظرم بهتر است که پیغام تابع getPuzzle را با یک عدد صحیح تعریف کنید (کلمه int که مخفف integer است و به معنای کل اعداد صحیح است). در ادامه عنصر portType را مشاهده میکنید، ولی فعلاً نگران این نباشید! در حال حاضر فقط با محتوای اصلی در این کد سرو کار داریم، که در تابع ( ) getpuzzle در کد اصلی PHP این متن نوشته شده است. این دستور، پیغام گرفته شده را به اجرا در می آورد. در ادامه بقیه کد ها را مشاهده میکنید:
</span></p> <p dir="ltr"><span style="font-family:tahoma,geneva,sans-serif;"><span style="font-size:16px;"><span class="geshifilter" style="margin: 0px; padding: 0px; border: none; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; display: inline;"><code class="xml geshifilter-xml" style="margin: 0px; padding: 0px; border: 0px; font-family: 'Source Code Pro', sans-serif; font-size: 1em; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline;"><span class="sc3" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(129, 194, 103);"><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;binding</span>&nbsp;<span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(41, 174, 243);">name</span>=<span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(229, 179, 36);">&quot;NetPuzzleBinding&quot;</span><span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(41, 174, 243);">type</span>=<span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(229, 179, 36);">&quot;typens:NetPuzzlePort&quot;</span><span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&gt;</span><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;soap:binding</span><span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(41, 174, 243);">style</span>=<span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(229, 179, 36);">&quot;rpc&quot;</span><span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(41, 174, 243);">transport</span>=<span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(229, 179, 36);">&quot;http://schemas.xmlsoap.org/soap/http&quot;</span><span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">/&gt;</span><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;operation</span>&nbsp;<span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(41, 174, 243);">name</span>=<span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(229, 179, 36);">&quot;getPuzzle&quot;</span><span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&gt;</span><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;soap:operation</span><span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(41, 174, 243);">soapAction</span>=<span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(229, 179, 36);">&quot;urn:NetPuzzleAction&quot;</span>&nbsp;<span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">/&gt;</span><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;input<span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline;">&gt;</span></span><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;soap:body</span>&nbsp;<span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(41, 174, 243);">use</span>=<span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(229, 179, 36);">&quot;encoded&quot;</span><span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(41, 174, 243);">namespace</span>=<span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(229, 179, 36);">&quot;urn:NetPuzzle&quot;</span><span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(41, 174, 243);">encodingStyle</span>=<span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(229, 179, 36);">&quot;http://schemas.xmlsoap.org/soap/encoding/&quot;</span><span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">/&gt;</span><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;/input<span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline;">&gt;</span></span><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;output<span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline;">&gt;</span></span><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;soap:body</span>&nbsp;<span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(41, 174, 243);">use</span>=<span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(229, 179, 36);">&quot;encoded&quot;</span><span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(41, 174, 243);">namespace</span>=<span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(229, 179, 36);">&quot;urn:NetPuzzle&quot;</span><span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(41, 174, 243);">encodingStyle</span>=<span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(229, 179, 36);">&quot;http://schemas.xmlsoap.org/soap/encoding/&quot;</span><span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">/&gt;</span><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;/output<span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline;">&gt;</span></span><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;/operation<span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline;">&gt;</span></span><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;/binding<span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline;">&gt;</span></span><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;service</span><span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(41, 174, 243);">name</span>=<span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(229, 179, 36);">&quot;NetPuzzleService&quot;</span><span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&gt;</span><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;port</span><span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(41, 174, 243);">name</span>=<span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(229, 179, 36);">&quot;NetPuzzlePort&quot;</span><span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(41, 174, 243);">binding</span>=<span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(229, 179, 36);">&quot;typens:NetPuzzleBinding&quot;</span><span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&gt;</span><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;soap:address</span><span class="re0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(41, 174, 243);">location</span>=<span class="st0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(229, 179, 36);">&quot;http://yourserver.com/server1.php&quot;</span>&nbsp;<span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">/&gt;</span><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;/port<span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline;">&gt;</span></span><span class="re1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;/service<span class="re2" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; line-height: inherit; vertical-align: baseline;">&gt;</span></span></span></code></span></span></span></p> <p style="margin: 0px 0px 20px; padding: 0px 25px 0px 0px; border: 0px; font-family: inherit; font-size: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 30.600000381469727px; vertical-align: baseline; text-align: justify;"><span style="font-size:16px;">
کل عبارت مرتبط با عنصر blinding میتواند نادیده گرفته شود. کار اصلی این عنصر، این است که برای ورودی و خروجی دستورات تابع getpuzzle استفاده میشود، اما در قالب اصلی xml، اگر بخواهیم آن را کامل کنیم باید بیشتر از 10 خط کد به توضیحات آن اضافه کنیم. اگر تصمیم دارید که بعداً عملیات خاص خودتان را به این کد اضافه کنید، فقط کافی است که این قطعه کد را کپی و پیست کنید. هیچ احتیاجی به تغییر کد در نام متغیرها و دستورالعمل اجرای آن ندارید.
در آخر، عنصر Service را مشاهده میکنید، که بسیار مهم است. در واقع این کد همان بخشی است که در اینترنت اجرا میشود. عنصر portType که قبلاً به آن اشاره کردیم در این قطعه کد به شما کمک میکند تا اسکریپت هایی که میخواهید برای اجرای کد تعریف کنید و در اینجا از آن استفاده کنید.
تا اینجا شما پارامترهای اصلی خودتان را در اختیار دارید، توابع اجرایی (operation)، پروتکل (binding)، و آدرس URL سایت (service)، که این نشان میدهد حالا شما آماده هستید که تا عملیات را کامل کنید. خب حالا میتوانید XML را پشت سر بگذارید و شروع به ساخت کدهای PHP کنید.

client/server

بعد از نوشتن کدهای XML شاید از دیدن این قطعه کد خنده تان بگیرد :
</span></p> <p dir="ltr"><span style="font-family:tahoma,geneva,sans-serif;"><span style="font-size:16px;"><span class="geshifilter" style="margin: 0px; padding: 0px; border: none; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; display: inline;"><code class="php geshifilter-php" style="margin: 0px; padding: 0px; border: 0px; font-family: 'Source Code Pro', sans-serif; font-size: 1em; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline;"><span class="sy1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;?</span>php<span class="sy0" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(129, 194, 103);">&amp;</span><span class="co2" style="margin: 0px; padding: 0px; border: 0px; font-style: italic; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(153, 153, 153);">#36;soap = new SoapClient(&quot;dotnet1.wsdl&quot;);echo &amp;#36;soap-&gt;getPuzzle(&quot;1&quot;);</span><span class="sy1" style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">?&gt;</span></code></span></span></span></p> <p style="margin: 0px 0px 20px; padding: 0px 25px 0px 0px; border: 0px; font-family: inherit; font-size: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 30.600000381469727px; vertical-align: baseline; text-align: justify;"><span style="font-size:16px;">
دوخط از این کد php همان کدی است که در API بارگذاری میشود، تابع را فراخوانی و نتیجه را چاپ کنید. این کد را در فایلی به نام  1.php در دایرکتوری فایل WSDL ذخیره کنید. این کد یک پازل ساده را از سرور اصلی فراخوانی میکند، پس شما نیاز دارید تا ترکیب آخر API را برنامه نویسی کنید، یعنی بخش سرور. برای اینکار احتیاج دارید تا درخواست Soap را قبول کنید. تابع ( )getPuzzle را کنترل کنید و بعد خروجی پازل را برای کاربر نمایش دهید. PHP بخش سخت را برای شما انجام داده است، که بخش کوتاه شده ی آن را در ادامه میبینید:
&nbsp;&nbsp;</span></span></p> <p dir="ltr"><span style="font-size:16px;"><span class="geshifilter" style="margin: 0px; padding: 0px; border: none; font-family: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; display: inline;"><code class="php geshifilter-php" style="margin: 0px; padding: 0px; border: 0px; font-family: 'Source Code Pro', sans-serif; font-size: 1em; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline;"><span class="sy1" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">&lt;?</span>phpfunction getPuzzle<span class="br0" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(129, 194, 103);">(</span><span class="sy0" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(129, 194, 103);">&amp;</span><span class="co2" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit; font-style: italic; font-variant: inherit; font-weight: inherit; line-height: inherit; vertical-align: baseline; color: rgb(153, 153, 153);">#36;difficulty) {&amp;#36;puzzles[1][] = &quot;What is the best web mag in the world?&quot;;&amp;#36;puzzles[2][] = &quot;What is the air-speed velocity of an unladen swallow?&quot;;&amp;#36;puzzles[3][] = &quot;What is the meaning of life?&quot;;&amp;#36;randpuz = array_rand(&amp;#36;puzzles[&amp;#36;difficulty]);return &amp;#36;puzzles[&amp;#36;difficulty][&amp;#36;randpuz];}&amp;#36;server = new SoapServer(&quot;dotnet1.wsdl&quot;);&amp;#36;server-&gt;addFunction(&quot;getPuzzle&quot;);&amp;#36;server-&gt;handle();</span><span class="sy1" style="margin: 0px; padding: 0px; border: 0px; font-family: inherit; font-style: inherit; font-variant: inherit; font-weight: bold; line-height: inherit; vertical-align: baseline; color: rgb(136, 136, 136);">?&gt;</span></code></span></span></p> <p style="margin: 0px 0px 20px; padding: 0px 25px 0px 0px; border: 0px; font-family: inherit; font-size: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 30.600000381469727px; vertical-align: baseline; text-align: justify;">&nbsp;</p> <p style="margin: 0px 0px 20px; padding: 0px 25px 0px 0px; border: 0px; font-family: inherit; font-size: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 30.600000381469727px; vertical-align: baseline; text-align: justify;"><span style="font-size:16px;">
اکثر اسکریپت های این کد برای تابع ( )getpuzzle تهیه شده است، که پارامتری را برای ذخیره درخواست متفاوتی اختصاص داده است. این تابع پازل اصلی شما را در آرایه ی $puzzles ذخیره کرده است، اما اگر میخواهید آن را توسعه دهید، باید یک پایگاه داده SQL با یک query مشابه برای آن در نظر بگیرید:
</span></p> <p style="margin: 0px 0px 20px; padding: 0px 25px 0px 0px; border: 0px; font-family: inherit; font-size: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 30.600000381469727px; vertical-align: baseline; text-align: justify;"><span style="font-size:16px;">SELECT Question FROM puzzles WHERE Difficulty = $difficulty ORDER BY rand()<br /> LIMIT 1;</span></p> <p style="margin: 0px 0px 20px; padding: 0px 25px 0px 0px; border: 0px; font-family: inherit; font-size: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 30.600000381469727px; vertical-align: baseline; text-align: justify;"><span style="font-size:16px;">
تابع موردنظر مقدار Puzzle را با استفاده از پرسیدن یک سوال راجب میزان سختی پازل برمیگرداند. گرچه میزان سختی همه ی پازل ها در این کد فقط مربوط به یک سوال است، پس میزانی که برمیگردد همواره ثابت خواهد بود!
سه خط آخر کد در واقع برای فراخوانی اسکریپت Soap از کد PHP از تابع ( )Puzzle است. در اینجا شما باید تابع ( )addFunction را برای هر اسکریپت Soap/PHP که احتیاج دارید ثبت کنید. وقتی کارتان تمام شد، فقط کافیست تا براحتی تابع ( )handle را فراخوانی کنید تا کد ورودی Soap در ساختار PHP را تجزیه و تحلیل کند. میدونم که میخواهید هرچه سریعتر این کد را اجرا کنید، اما اول باید کد WSDL cache را در PHP غیرفعال کنید. شما از قبل دیدید که درخواست تابع wsdl بسیار طولانی خواهد بود، بنابراین برای اجتناب از تجزیه و تحلیل کردن و ارزش گذاری تک تک عناصر واقع در کد، میتوانید از PHP cache واقع در WSDL برا ی تجزیه و تحلیل کردن استفاده کنید. در حالیکه در مرحله توسعه هستید، این مشکلات پیش خواهد آمد. با اینحال اگر هر تغییری در ساختار فایل WSDL ایجاد کنید هیچ اثری در php به جا نخواهد گذاشت، چون این کد هنوز از ورژن cache اصلی استفاده خواهد کرد. برای تغییر دادن میتوانید از این روش استفاده کنید:
به طور موقتی PHP caching سیستم را غیرفعال کنید( که میتوانید در این قسمت آن را غیرفعال کنید: به فایل php.ini مراجعه کنید و به دنبال این عبارت بگردید: soap.wsdl_cache_enabled، سپس کل عبارت را به "O" تغییر دهید.
بعد از اعمال تغییرات (اگر از ماژول php استفاده میکنید فایل Apache سرور را ریستارت کنید)، اسکریپت 1.php را مجددا بارگزاری کنید. بعد از اینکار باید این پیغام را مشاهده کنید: "?What is the best web mag in the world" این پیغام نشان میدهد که کاربر ما بعد از اجرای فایل این پیغام را از سرور دریافت خواهد کرد.
چند نکته مهم
قبل از اینکه بحث را تمام کنیم، سه نکته مهم است که میخواهم به آن اشاره کنم تا به شما در بالا نگه داشتن WSDL کمک کند:
اول اینکه اطمینان حاصل کنید که API پایدار باشد. منظورم این است که مثلاً اگر کاربران تابع ( )getPuzzle را فرخوانی کردند، تغییری در ساختار کد به وجود نیامده باشد، پارامتر جدید به آن اضافه یا از آن حذف نشده باشد، نوع داده اصلی آن تغییری نکند و در کل اینکه عملکرد کلی آن خراب و غیرقابل اجرا نباشد. این به این معناست که شما بعد از انتشار کد این حق را ندارید که کد PHP را بازنویسی کنید و کاری کنید که تابع getPuzzle کار دیگری انجام دهد. با اینحال اگر هم قصد تغییر کد را دارید مطمئن شوید که تابع جدیدی تولید کنید، مثلاً ( )getPuzzle_2
دومین نکته اینکه یادتان باشد به کاربران دیگر درباره ی تغییرات WSDL هشدار دهید تا آنها بدانند که باید ورژن جدید برنامه را دانلود کنند.
و نکته ی آخر اینکه درست است که در کدهای PHP فرقی بین متغیرهای صحیح و رشته ها وجود ندارد، اما در زبان های برنامه نویسی دیگر این تفاوت بدیهی است، پس از کدهای درستی در ساختار برنامه استفاده کنید. تا زمانی که wsdl بیشتری را پردازش میکنید، ارزش آن را بیشتر درک میکنید، چون PHP این توانایی را دارد تا تشخیص دهد که شما چه محتوایی را صحیح یا غلط ارسال میکنید.

مطالب مرتبط


اشتراک گذاری:
021-41658