详解php用curl调用接口方法,get和post两种方式

前端之家收集整理的这篇文章主要介绍了详解php用curl调用接口方法,get和post两种方式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先是客户端执行方法ApiModel.PHP

PHP;"> $o = ""; foreach ( $post_data as $k => $v ) { $o.= "$k=" . urlencode( $v ). "&" ; } $post_data = substr($o,-1); $key=md5(base64_encode($post_data)); if($ispost){ $url=$url; }else{ $url = $url.'?'.$post_data; } $curlPost = 'key='.$key; header("Content-type: text/html; charset=utf-8"); $ch = curl_init();//初始化curl curl_setopt($ch,CURLOPT_URL,$url);//抓取指定网页 curl_setopt($ch,CURLOPT_HEADER,0);//设置header curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//要求结果为字符串且<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>到屏幕上 if($ispost){ curl_setopt($ch,CURLOPT_POST,1);//post提交方式 curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost); } $data = curl_exec($ch);//运行curl curl_close($ch); return $data;

}
?>

客户端调用方法,可以在此配置基本信息api.PHP

PHP;"> }
testAction();
?>

服务器的接口函数test.PHP

PHP;"> 'huanglu','password'=>'123456'); echo json_encode($arr); }else{ exit('非法访问!'); } } serverapi(); ?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

原文链接:https://www.f2er.com/php/18108.html

猜你在找的PHP相关文章