欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php使用socket post數(shù)據(jù)到其它web服務(wù)器的方法

 更新時(shí)間:2015年06月02日 10:29:09   作者:不吃皮蛋  
這篇文章主要介紹了php使用socket post數(shù)據(jù)到其它web服務(wù)器的方法,涉及php使用socket傳輸數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了php使用socket post數(shù)據(jù)到其它web服務(wù)器的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

function post_request($url, $data, $referer='') {
  // Convert the data array into URL Parameters like a=b&foo=bar etc.
  $data = http_build_query($data);
  // parse the given URL
  $url = parse_url($url);
  if ($url['scheme'] != 'http') { 
    die('Error: Only HTTP request are supported !');
  }
  // extract host and path:
  $host = $url['host'];
  $path = $url['path'];
  // open a socket connection on port 80 - timeout: 30 sec
  $fp = fsockopen($host, 80, $errno, $errstr, 30);
  if ($fp){
    // send the request headers:
    fputs($fp, "POST $path HTTP/1.1\r\n");
    fputs($fp, "Host: $host\r\n");
    if ($referer != '')
      fputs($fp, "Referer: $referer\r\n");
    fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
    fputs($fp, "Content-length: ". strlen($data) ."\r\n");
    fputs($fp, "Connection: close\r\n\r\n");
    fputs($fp, $data);
    $result = ''; 
    while(!feof($fp)) {
      // receive the results of the request
      $result .= fgets($fp, 128);
    }
  }
  else { 
    return array(
      'status' => 'err', 
      'error' => "$errstr ($errno)"
    );
  }
  // close the socket connection:
  fclose($fp);
  // split the result header from the content
  $result = explode("\r\n\r\n", $result, 2);
  $header = isset($result[0]) ? $result[0] : '';
  $content = isset($result[1]) ? $result[1] : '';
  // return as structured array:
  return array(
    'status' => 'ok',
    'header' => $header,
    'content' => $content
  );
}
//使用方法
// Submit those variables to the server
$post_data = array(
  'test' => 'foobar',
  'okay' => 'yes',
  'number' => 2
);
// Send a request to example.com 
$result = post_request('http://www.example.com/', $post_data);
if ($result['status'] == 'ok'){
  // Print headers 
  echo $result['header']; 
  echo '<hr />';
  // print the result of the whole request:
  echo $result['content'];
}
else {
  echo 'A error occured: ' . $result['error']; 
}

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • PHP ajax跨子域的解決方案之document.domain+iframe實(shí)例分析

    PHP ajax跨子域的解決方案之document.domain+iframe實(shí)例分析

    這篇文章主要介紹了PHP ajax跨子域的解決方案之document.domain+iframe,結(jié)合實(shí)例形式分析了PHP ajax跨子域的解決方案document.domain+iframe的基本原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下
    2020-03-03
  • PHP大轉(zhuǎn)盤中獎(jiǎng)概率算法實(shí)例

    PHP大轉(zhuǎn)盤中獎(jiǎng)概率算法實(shí)例

    這篇文章主要介紹了PHP大轉(zhuǎn)盤中獎(jiǎng)概率算法,實(shí)例講述了概率算法的實(shí)現(xiàn)過程,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-10-10
  • PHP文件上傳安全:優(yōu)化代碼有效防范漏洞

    PHP文件上傳安全:優(yōu)化代碼有效防范漏洞

    PHP文件上傳安全是網(wǎng)站開發(fā)中至關(guān)重要的一環(huán),想要避免惡意攻擊和數(shù)據(jù)泄露?本指南將為您揭示優(yōu)化代碼、有效防范漏洞的關(guān)鍵方法,讓我們一起打造更安全的文件上傳系統(tǒng)吧!
    2023-12-12
  • PHP使用Curl實(shí)現(xiàn)模擬登錄及抓取數(shù)據(jù)功能示例

    PHP使用Curl實(shí)現(xiàn)模擬登錄及抓取數(shù)據(jù)功能示例

    這篇文章主要介紹了PHP使用Curl實(shí)現(xiàn)模擬登錄及抓取數(shù)據(jù)功能,結(jié)合實(shí)例形式分析了php使用curl進(jìn)行登陸、驗(yàn)證、cookie操作與數(shù)據(jù)抓取等相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-04-04
  • php 數(shù)組操作(增加,刪除,查詢,排序)等函數(shù)說明

    php 數(shù)組操作(增加,刪除,查詢,排序)等函數(shù)說明

    php 數(shù)組增加,刪除,查詢,排序詳細(xì)說明,需要的朋友可以參考下。
    2010-05-05
  • 詳解Laravel框架的依賴注入功能

    詳解Laravel框架的依賴注入功能

    依賴注入不是讓對(duì)象創(chuàng)建一個(gè)依賴關(guān)系,也不是讓工廠對(duì)象去創(chuàng)建對(duì)象,而是將所需的依賴變成一個(gè)外部對(duì)象,使之成為一個(gè)"某些人的問題”,你為"某些人的問題”注入了類的依賴關(guān)系。在Laravel中,這個(gè)"某人”是服務(wù)容器,服務(wù)容器負(fù)責(zé)通過構(gòu)造函數(shù)注入類的依賴關(guān)系。
    2021-05-05
  • 火車頭采集器3.0采集圖文教程

    火車頭采集器3.0采集圖文教程

    今天要給大家做示例的網(wǎng)站是163的 娛樂頻道 這個(gè)應(yīng)該是個(gè)比較通用和實(shí)用的規(guī)則,下面開始。
    2007-03-03
  • 設(shè)置php頁面編碼的兩種方法示例介紹

    設(shè)置php頁面編碼的兩種方法示例介紹

    這篇文章主要介紹了兩種設(shè)置php頁面編碼的方法,并附有示例,大家可以練習(xí)下
    2014-03-03
  • php開發(fā)中的頁面跳轉(zhuǎn)方法總結(jié)

    php開發(fā)中的頁面跳轉(zhuǎn)方法總結(jié)

    PHP頁面跳轉(zhuǎn)實(shí)現(xiàn)的功能就是將網(wǎng)站中一個(gè)網(wǎng)頁跳轉(zhuǎn)到另一個(gè)網(wǎng)頁中。對(duì)于剛剛學(xué)習(xí)PHP語言的朋友來說,是必須要掌握的基礎(chǔ)方法。
    2015-04-04
  • PHP Directory 函數(shù)的詳解

    PHP Directory 函數(shù)的詳解

    PHP Directory 函數(shù)的詳解 ,需要的朋友可以參考一下
    2013-03-03

最新評(píng)論