php分别模拟发送GET和POST请求

PHP 投稿 10500 1 评论

php分别模拟发送GET和POST请求

php分别模拟发送GET和POST请求,非常实用的额,也可作PHP CURL入门级的理解教材的,示例代码如下:


<?php
/*
**  php分别模拟发送GET与POST请求
**
**  www.feishuai.vip 2012-07-28 23:02:07
*/

function httpRequest($url,$method,$params=array()){
	if(trim($url)==''||!in_array($method,array('get','post'))||!is_array($params)){
		return false;
	}
	$curl=curl_init();
	curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($curl,CURLOPT_HEADER,0 ) ;
	switch($method){
		case 'get':
			$str='?';
			foreach($params as $k=>$v){
				$str.=$k.'='.$v.'&';
			}
			$str=substr($str,0,-1);
			$url.=$str;//$url=$url.$str;
			curl_setopt($curl,CURLOPT_URL,$url);
		break;
		case 'post':
			curl_setopt($curl,CURLOPT_URL,$url);
			curl_setopt($curl,CURLOPT_POST,1 );
			curl_setopt($curl,CURLOPT_POSTFIELDS,$params);
		break;
		default:
			$result='';
		break;
	}
	if(isset($result)){
		$result=curl_exec($curl);
	}
	curl_close($curl);
	return $result;
}

编程笔记 » php分别模拟发送GET和POST请求

赞同 (56) or 分享 (0)
游客 发表我的评论   换个身份
取消评论

表情
(1)个小伙伴在吐槽
  1. 设计很美观。
    魔法指挥官 2023-09-29 06:30 (1年前) 回复