本文主要是介绍PHP----curl默默http的get跟post请求,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
开始php_curl扩展,php.ini文件开启
本用的是PHP 5.5版本,官网自带的php_curl.dll是错误的,学重新找过,才有效
重启apache
GET请求
1.php
<?phpheader('Content-type: text/html; charset: utf-8');$curl = curl_init();$url = "http://www.hjh.com/2017.1.13/2.php?text=哈哈哈哈哈";curl_setopt($curl , CURLOPT_SSL_VERIFYPEER , false);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST , false);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);$res = curl_exec($curl);curl_close($curl);var_dump($res);?>
2.php
<?phpheader('Content-type: text/html; charset: utf-8');if($_GET){$id = $_GET['id'];file_put_contents('1.text', $id , FILE_APPEND );}
POST请求
1.php
<?phpheader('Content-type: text/html; charset: utf-8');$curl = curl_init();$url = "http://www.hjh.com/2017.1.13/2.php";$data = ['text'=>'我是post过来的数据'];curl_setopt($curl , CURLOPT_URL , $url);curl_setopt($curl , CURLOPT_SSL_VERIFYPEER , false);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST , false);curl_setopt($curl,CURLOPT_POST,1);curl_setopt($curl,CURLOPT_POSTFIELDS,$data);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);$res = curl_exec($curl);curl_close($curl);var_dump($res);?>
2.php
<?phpheader('Content-type: text/html; charset: utf-8');if($_POST){$t = $_POST['text'];echo __FILE__;var_dump($t) ;file_put_contents('2.text', $t , FILE_APPEND );}
?>
最后白curl封装成一个方法
// curl函数public function getCurl($url , $data = null){$curl = curl_init();// 数据请求的url$curl = curl_opt($cul ,CURLOPT_URL ,$url);// 关闭证书,域名验证curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER , false);curl_setopt( $curl , CURLOPT_SSL_VERIFYHOST, false);// 判断是否有数据if($data){curl_setopt( $curl , CURLOPT_POST , 1);curl_setopt( $curl , CURLOPT_POSTFIELDS , $data);}// 接受返回数据curl_opt( $curl , CURLOPT_RETURNTRANSFER , 1);// 执行curl$optput = curl_exec($curl);curl_close($curl);return $optput}
这篇关于PHP----curl默默http的get跟post请求的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!