国产探花免费观看_亚洲丰满少妇自慰呻吟_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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贵德县| 阿拉善左旗| 连山| 赞皇县| 西乌珠穆沁旗| 兴宁市| 尼木县| 五河县| 甘德县| 涪陵区| 道孚县| 财经| 黄骅市| 淳安县| 广昌县| 滕州市| 临泉县| 苏尼特右旗| 泗阳县| 阿合奇县| 阿瓦提县| 宾阳县| 义乌市| 石首市| 石河子市| 天长市| 英德市| 南京市| 普洱| 深泽县| 京山县| 蒙城县| 大同市| 仁布县| 山阳县| 湖南省| 黔南| 宁夏| 彭阳县| 洮南市| 江阴市|