มาลองเขียน PHPCurl เรียกหน้า ASP แบบง่าย ๆ กัน

หลาย ๆ คนอาจจะรู้จักแล้ว ว่า cURL คืออะไร แต่ส่วนตัว เพิ่งเคยใช้นี่ละ :P

cURL เป็น tool ที่ใช้สำหรับในการ transfer ข้อมูลของ protocol ต่าง ๆ อย่าง HTTP HTTPS FTP SSH เป็นต้น

ตัวอย่างการใช้งานที่ผมจะลองใช้คือ มีเว็บนึง ต้องการการ login ก่อน จากนั้นจะสามารถเข้าใช้งานฟังก์ชันต่าง ๆ ในเว็บได้
ผมจะลองใช้ cURL ในการเรียกใช้ครับ ตัว cURL จะทำหน้าที่คล้าย ๆ เป็น web browser หรือ client ที่ browse ไปยังหน้าต่าง ๆ ให้

สำหรับ หน้า aspx จะมี FIELD ที่ชื่อ __VIEWSTATE อยู่เป็น ID ประจำหน้า ซึ่งก็ไม่รู้เหมือนกันมันคืออะไร
เพราะไม่สันทัด แต่เอาเป็นว่า ต้องใส่ละกัน

<?php
// สร้าง cookie
$cookie_file_path "/path/to/cookie.txt";
$LOGINURL "http://www.example.com/login.aspx";
$agent "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";

// ทำการ login 
$ch curl_init();
curl_setopt($chCURLOPT_URL,$LOGINURL);
curl_setopt($chCURLOPT_USERAGENT$agent);
curl_setopt($chCURLOPT_RETURNTRANSFER1);
curl_setopt($chCURLOPT_FOLLOWLOCATION1);
curl_setopt($chCURLOPT_COOKIEFILE$cookie_file_path);
curl_setopt($chCURLOPT_COOKIEJAR$cookie_file_path);
$result curl_exec ($ch);
curl_close ($ch);
unset(
$ch); 

// ค้นหา view state ปัจจุบัน
preg_match('/<input type="hidden" name="__VIEWSTATE" value="([^";]*?)"; \/>/'$result$matches);
$viewstate $matches[1];
$viewstate urlencode($viewstate);

//ทำการเรียกใช้งานฟังก์ชั่นที่ต้องการ
$POSTURL "http://www.example.com/main.aspx";
$reffer "http://www.example.com/main.aspx";
$POSTFIELDS "__EVENTTARGET=&amp;__EVENTARGUMENT=&__VIEWSTATE=$viewstate&txtARG1=$arg1&txtARG2=$arg2";

$ch curl_init();
curl_setopt($chCURLOPT_URL,$POSTURL);
curl_setopt($chCURLOPT_USERAGENT$agent);
curl_setopt($chCURLOPT_POST1);
curl_setopt($chCURLOPT_POSTFIELDS,$POSTFIELDS);
curl_setopt($chCURLOPT_RETURNTRANSFER1);
curl_setopt($chCURLOPT_FOLLOWLOCATION1);
curl_setopt($chCURLOPT_REFERER$reffer);
curl_setopt($chCURLOPT_COOKIEFILE$cookie_file_path);
curl_setopt($chCURLOPT_COOKIEJAR$cookie_file_path);
$result curl_exec ($ch);<br /><br />curl_close ($ch);
unset(
$ch);
echo 
$result;

// ค้นหา view state ปัจจุบัน
preg_match('/<input type="hidden"; name="__VIEWSTATE" value="([^"]*?)"; \/>/'$result$matches)
$viewstate $matches[1];
$viewstate urlencode($viewstate);
//Log out
$LOGOFFURL "http://www.example.com/logOff.aspx";
$agent "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$ch curl_init();
curl_setopt($chCURLOPT_URL,$LOGOFFURL);
curl_setopt($chCURLOPT_USERAGENT$agent);
curl_setopt($chCURLOPT_RETURNTRANSFER1);
curl_setopt($chCURLOPT_FOLLOWLOCATION1);
curl_setopt($chCURLOPT_COOKIEFILE$cookie_file_path)
curl_setopt($chCURLOPT_COOKIEJAR$cookie_file_path);
$result curl_exec ($ch);
curl_close ($ch);
unset(
$ch);
?>

แค่นี้เราก็สามารถเรียก wrapper สำหรับ ใช้กับเว็บเราได้แล้วครับ :D

ความเห็น

Server Error in '/' Application.
--------------------------------------------------------------------------------

Invalid character in a Base-64 string.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Invalid character in a Base-64 string.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[FormatException: Invalid character in a Base-64 string.]
System.Convert.FromBase64String(String s) +0
System.Web.UI.LosFormatter.Deserialize(String input) +25
System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +101

[HttpException (0x80004005): Invalid_Viewstate
Client IP: 125.24.71.11
Port: 54499
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)
ViewState: dDwxODM4ODk4MDYyO3Q8O2w8aTwxPjs... (จริงๆ มีตัวอักษรอะไรก็ไม่รู้ยาวมาก เลยตัดมานิดเดียวเพราะ ข้อความยาวไป)
Http-Referer: http://www.efinancethai.com/
Path: /index.aspx.]
System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +442
System.Web.UI.Page.LoadPageViewState() +18
System.Web.UI.Page.ProcessRequestMain() +441

รบกวนช่วยด้วยนะครับ ต้องทำงานส่งอาจารย์ ไม่งั้นคง... ผมพยามหาจาก google แล้วครับ แต่ไม่ได้สักที ในเว็บที่เป็น asp บางเว็บดึงข้อมูลได้ครับ แต่ที่เข้ารหัสไม่ได้นะครับ ไม่ต้องเขียนยังไง