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

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

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

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

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

  1. // สร้าง cookie
  2. $cookie_file_path = "/path/to/cookie.txt";
  3. $LOGINURL = "http://www.example.com/login.aspx";
  4. $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
  5.  
  6. // ทำการ login
  7. $ch = curl_init();
  8. curl_setopt($ch, CURLOPT_URL,$LOGINURL);
  9. curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  10. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  11. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  12. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
  13. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
  14. $result = curl_exec ($ch);
  15. curl_close ($ch);
  16. unset($ch);
  17.  
  18. // ค้นหา view state ปัจจุบัน
  19. preg_match('/<input type="hidden" name="__VIEWSTATE" value="([^";]*?)"; \/>/', $result, $matches);
  20. $viewstate = $matches[1];
  21. $viewstate = urlencode($viewstate);
  22.  
  23. //ทำการเรียกใช้งานฟังก์ชั่นที่ต้องการ
  24. $POSTURL = "http://www.example.com/main.aspx";
  25. $reffer = "http://www.example.com/main.aspx";
  26. $POSTFIELDS = "__EVENTTARGET=&amp;__EVENTARGUMENT=&__VIEWSTATE=$viewstate&txtARG1=$arg1&txtARG2=$arg2";
  27.  
  28. $ch = curl_init();
  29. curl_setopt($ch, CURLOPT_URL,$POSTURL);
  30. curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  31. curl_setopt($ch, CURLOPT_POST, 1);
  32. curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS);
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  34. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  35. curl_setopt($ch, CURLOPT_REFERER, $reffer);
  36. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
  37. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
  38. $result = curl_exec ($ch);<br /><br />curl_close ($ch);
  39. unset($ch);
  40. echo $result;
  41.  
  42. // ค้นหา view state ปัจจุบัน
  43. preg_match('/<input type="hidden"; name="__VIEWSTATE" value="([^"]*?)"; \/>/', $result, $matches)
  44. $viewstate = $matches[1];
  45. $viewstate = urlencode($viewstate);
  46. //Log out
  47. $LOGOFFURL = "http://www.example.com/logOff.aspx";
  48. $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
  49. $ch = curl_init();
  50. curl_setopt($ch, CURLOPT_URL,$LOGOFFURL);
  51. curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  52. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  53. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  54. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path)
  55. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
  56. $result = curl_exec ($ch);
  57. curl_close ($ch);
  58. unset($ch);

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


ความคิดเห็น

แสดงความคิดเห็น

Google Friend Connect (leave a quick comment)
loading...
เนื้อหาของข้อมูลนี้ถูกรักษาเป็นความลับและไม่แสดงต่อสาธารณะ
CAPTCHA
คำถามนี้เป็นการทดสอบว่าท่านเป็น spam หรือไม่
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.