本文转自:https://www.cnblogs.com/jiqing9006/p/3949190.html

 /**
* 模拟post进行url请求
* @param string $url
* @param string $param
*/
function request_post($url = '', $param = '') {
if (empty($url) || empty($param)) {
return false;
} $postUrl = $url;
$curlPost = $param;
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//运行curl
curl_close($ch); return $data;
}

这是方法,

下面是具体的调用案例。

    function testAction(){
$url = 'http://mobile.jschina.com.cn/jschina/register.php';
$post_data['appid'] = '10';
$post_data['appkey'] = 'cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ';
$post_data['member_name'] = 'zsjs123';
$post_data['password'] = '123456';
$post_data['email'] = 'zsjs123@126.com';
$o = "";
foreach ( $post_data as $k => $v )
{
$o.= "$k=" . urlencode( $v ). "&" ;
}
$post_data = substr($o,0,-1); $res = $this->request_post($url, $post_data);
print_r($res); }

这样就提交请求,并且获取请求结果了。一般返回的结果是json格式的。

这里的post是拼接出来的。

也可以改造成下面的方式。

/**
* 模拟post进行url请求
* @param string $url
* @param array $post_data
*/
function request_post($url = '', $post_data = array()) {
if (empty($url) || empty($post_data)) {
return false;
} $o = "";
foreach ( $post_data as $k => $v )
{
$o.= "$k=" . urlencode( $v ). "&" ;
}
$post_data = substr($o,0,-1); $postUrl = $url;
$curlPost = $post_data;
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//运行curl
curl_close($ch); return $data;
}

将拼接也封装了起来,这样调用的时候就更简洁了。

function testAction(){
$url = 'http://mobile.jschina.com.cn/jschina/register.php';
$post_data['appid'] = '10';
$post_data['appkey'] = 'cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ';
$post_data['member_name'] = 'zsjs124';
$post_data['password'] = '123456';
$post_data['email'] = 'zsjs124@126.com';
//$post_data = array();
$res = $this->request_post($url, $post_data);
print_r($res); }

[转]php模拟post提交请求,调用接口的更多相关文章

  1. php模拟post提交请求,调用接口

    /** * 模拟post进行url请求 * @param string $url * @param string $param */ function request_post($url = '', ...

  2. php模拟post提交请求与调用接口

    /** * 模拟post进行url请求 * @param string $url * @param string $param */ function request_post($url = '', ...

  3. 使用HttpClient配置代理服务器模拟浏览器发送请求调用接口测试

    在调用公司的某个接口时,直接通过浏览器配置代理服务器可以请求到如下数据: 请求url地址:http://wwwnei.xuebusi.com/rd-interface/getsales.jsp?cid ...

  4. java通过java.net.URL发送http请求调用接口

    一般在*.html,*.jsp页面中我们通过使用ajax调用接口,这个是我们通常用的.对于这些接口,大都是本公司写的接口供自己调用,所以直接用ajax就可以.但是,如果是多家公司共同开发一个东西,一个 ...

  5. 利用在线HTTP接口测试工具ApiPost,模拟移动端请求API接口

    我们做开发一般都是在PC端,然而我们很多时候需要模拟移动端进行接口请求. 比如,当一个API需要根据用户不同的终端来返回不同的数据时,就需要一个工具来模拟不同的终端了. 利用接口测试工具ApiPost ...

  6. php通过CURL模拟post提交请求

    <?php header("Content-type:text/html;charset=utf-8"); class Test{ public function reque ...

  7. C#发起Http请求,调用接口

    //方法1. Post 异步请求,普通的异步请求,传输普通的字符串等,对于有html代码的字段值的传输支持不好,如果需要传输html,二进制等数据的传输,请使用下面第二个方法,即使用UploadDat ...

  8. php 用post请求调用接口api

    $post_data=""; $ch = curl_init(); $url = ''; curl_setopt($ch , CURLOPT_URL , $url); curl_s ...

  9. php通过CURL模拟get提交请求

    方式一: $host = "http://jisunews.market.alicloudapi.com"; $path = "/news/get"; $met ...

随机推荐

  1. idea jetty 配置

    一.jetty 网址下载地:https://www.eclipse.org/jetty/ 1.如下图红色箭头--> 点击Downloads 下载 2.选择最新版 .ZIP 下载 3.选择安装路径 ...

  2. 《python语言程序设计》_第二章笔记之2.13_软件开发流程

    #程序1: 设计:由用户键入利率.贷款数以及贷款的年限,系统计算出每月还贷数和总还款数 注意:输入的年利率是带有百分比的数字,例如:4.5%.程序需要将它除以100转换成小数.因为一年有12个月,所以 ...

  3. 剑指offer面试题24:二叉搜索树的后序遍历序列

    题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是返回true,否则返回false. 假设输入的数组任意两个数字都不相同 解题思路:二叉搜索树的特点是根节点的左子树的值小于等 ...

  4. ZKWeb网页框架2.2正式发布

    2.2.0更新的内容有 重构依赖注入容器 使用无锁算法 (可能导致不兼容) 使用 ContainerFactoryData 创建实例 (可能导致不兼容) 删除静态的 ContainerFactoryC ...

  5. pycharm激活方式

    进入C:\Windows\System32\drivers\etc替换host文件,或者在host文件后加入0.0.0.0 account.jetbrains.com然后断网,断网,断网!最后输入K7 ...

  6. H5中用js让页面全屏

    概述 意外的发现一个网页有一个按钮能使网页全屏,查了下代码发现是H5的全屏api,于是就查了下资料,记录下来供以后开发时参考,相信对其他人也有用. 参考资料: 利用js如何做到让页面全屏和不全屏功能 ...

  7. Python开发之---PyCharm初体验

    PyCharm 的初始设置(知道) 目标 恢复 PyCharm 的初始设置 第一次启动 PyCharm 新建一个 Python 项目 设置 PyCharm 的字体显示 PyCharm 的升级以及其他 ...

  8. 解决 Chrome 下载不了东西 失败 - 已屏蔽 的问题

    或许你怎么也想不到是IE的问题 由于IE的安全设定问题 但是这个锅 确实不应该是IE来背. 因为我IE下载都没出现这个问题. 解决方法是这样的: IE>Internet选项>安全>自 ...

  9. odoo开发笔记--开启后台日志记录

    odoo后台日志记录功能 修改启动文件odoo.conf 将参数logfile注释放开, logfile = /var/log/odoo/odoo-server.log login_message = ...

  10. ubuntu16.04 服务器允许远程连接

    ubuntu默认安装了openssh-client,openssh-server需要手动安装. 查看是否安装了ssh服务 apt-cache policy openssh-client openssh ...