国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php使用socket post數據到其它web服務器的方法

2024-05-04 23:35:50
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php使用socket post數據到其它web服務器的方法,涉及php使用socket傳輸數據的相關技巧,需要的朋友可以參考下

本文實例講述了php使用socket post數據到其它web服務器的方法。分享給大家供大家參考。具體實現方法如下:

 

 
  1. function post_request($url$data$referer='') { 
  2. // Convert the data array into URL Parameters like a=b&foo=bar etc. 
  3. $data = http_build_query($data); 
  4. // parse the given URL 
  5. $url = parse_url($url); 
  6. if ($url['scheme'] != 'http') {  
  7. die('Error: Only HTTP request are supported !'); 
  8. // extract host and path: 
  9. $host = $url['host']; 
  10. $path = $url['path']; 
  11. // open a socket connection on port 80 - timeout: 30 sec 
  12. $fp = fsockopen($host, 80, $errno$errstr, 30); 
  13. if ($fp){ 
  14. // send the request headers: 
  15. fputs($fp"POST $path HTTP/1.1/r/n"); 
  16. fputs($fp"Host: $host/r/n"); 
  17. if ($referer != ''
  18. fputs($fp"Referer: $referer/r/n"); 
  19. fputs($fp"Content-type: application/x-www-form-urlencoded/r/n"); 
  20. fputs($fp"Content-length: "strlen($data) ."/r/n"); 
  21. fputs($fp"Connection: close/r/n/r/n"); 
  22. fputs($fp$data); 
  23. $result = '';  
  24. while(!feof($fp)) { 
  25. // receive the results of the request 
  26. $result .= fgets($fp, 128); 
  27. else {  
  28. return array
  29. 'status' => 'err',  
  30. 'error' => "$errstr ($errno)" 
  31. ); 
  32. // close the socket connection: 
  33. fclose($fp); 
  34. // split the result header from the content 
  35. $result = explode("/r/n/r/n"$result, 2); 
  36. $header = isset($result[0]) ? $result[0] : ''
  37. $content = isset($result[1]) ? $result[1] : ''
  38. // return as structured array: 
  39. return array
  40. 'status' => 'ok'
  41. 'header' => $header
  42. 'content' => $content 
  43. ); 
  44. //使用方法 
  45. // Submit those variables to the server 
  46. $post_data = array
  47. 'test' => 'foobar'
  48. 'okay' => 'yes'
  49. 'number' => 2 
  50. ); 
  51. // Send a request to example.com  
  52. $result = post_request('http://www.example.com/'$post_data); 
  53. if ($result['status'] == 'ok'){ 
  54. // Print headers  
  55. echo $result['header'];  
  56. echo '<hr />'
  57. // print the result of the whole request: 
  58. echo $result['content']; 
  59. else { 
  60. echo 'A error occured: ' . $result['error'];  

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 靖宇县| 西畴县| 凤台县| 阿拉善左旗| 景洪市| 扶沟县| 仲巴县| 肇州县| 通江县| 景泰县| 北安市| 玉山县| 洛宁县| 龙门县| 宝丰县| 浦江县| 沾化县| 长兴县| 辰溪县| 嵊泗县| 湄潭县| 江山市| 新蔡县| 禹城市| 蓬溪县| 镇巴县| 涿州市| 灌南县| 舞阳县| 井研县| 新郑市| 新田县| 商河县| 丰镇市| 明星| 光泽县| 双峰县| 正阳县| 永川市| 攀枝花市| 宜阳县|