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

首頁 > 語言 > PHP > 正文

php發(fā)送post請求函數(shù)分享

2024-09-04 11:43:25
字體:
供稿:網(wǎng)友

這篇文章主要介紹了一個php發(fā)送post請求的函數(shù),開發(fā)中經(jīng)常會用到,需要的朋友可以參考下,代碼如下:

  1. function do_post_request($url$data$optional_headers = null) 
  2.  $params = array('http' => array
  3. 'method' => 'POST'
  4. 'content' => $data 
  5.  )); 
  6.  if ($optional_headers !== null) { 
  7. $params['http']['header'] = $optional_headers
  8.  } 
  9.  $ctx = stream_context_create($params); 
  10.  $fp = @fopen($url'rb', false, $ctx); 
  11.  if (!$fp) { 
  12. throw new Exception("Problem with $url, $php_errormsg"); 
  13.  } //Vevb.com 
  14.  $response = @stream_get_contents($fp); 
  15.  if ($response === false) { 
  16. throw new Exception("Problem reading data from $url, $php_errormsg"); 
  17.  } 
  18.  return $response

用法如下:

  1. //json字符串 
  2. $data = "{...}"
  3. //轉(zhuǎn)換成數(shù)組 
  4. $data=json_decode($data,true); 
  5. $postdata = http_build_query($data); 
  6. do_post_request("http://localhost",$postdata); 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 鹰潭市| 象山县| 格尔木市| 繁昌县| 大庆市| 紫云| 仁怀市| 三明市| 松潘县| 布拖县| 古田县| 衡山县| 荃湾区| 新民市| 光泽县| 文安县| 普洱| 舒城县| 阿瓦提县| 兴安县| 固阳县| 桂林市| 枣强县| 佛冈县| 霍城县| 凤城市| 乌审旗| 鄯善县| 高平市| 林西县| 阜城县| 玉溪市| 浦县| 黄浦区| 平遥县| 会泽县| 浮山县| 灵宝市| 江川县| 宜黄县| 福建省|