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

 
更多

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

<?php
/*
**  php分别模拟发送GET与POST请求
**
**  www.phpernote.com 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;
}
打赏

本文固定链接: https://www.cxy163.net/archives/4142 | 绝缘体

该日志由 绝缘体.. 于 2013年05月23日 发表在 未分类 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: php分别模拟发送GET和POST请求 | 绝缘体
关键字: , , , ,

php分别模拟发送GET和POST请求:等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter