当前位置:首页 > PHP > 正文内容

PHP使用post发送Json数据

高老师9年前 (2017-07-02)PHP2388

前公司吃饭是需要在钉钉报餐的,对于不挑剔的我每天都在公司吃饭,有时忘记报餐导致吃不了饭还是很麻烦的。看了下报餐系统需要的是json数据包含我的工号即可。于是编写如下代码,放在360网址监控,1小时执行1次

<?php
/*自动报餐类*/
class  AutoBaocan{
 
private $url;
 
private $data;
 
 
//构造方法初始化
function __construct($url,$data){
$this->url=$url;
$this->data=$data;
}
 
 
 
//(1).创建会话,返回会话
public function newcurl(){
 
$ch=curl_init();//初始会话
 
curl_setopt($ch,CURLOPT_POST,1);/*Post模式*/
curl_setopt($ch,CURLOPT_URL,$this->url);/*URl设置*/
curl_setopt($ch,CURLOPT_POSTFIELDS,$this->data);/*Json数据*/
curl_setopt($ch,CURLOPT_HTTPHEADER,array(
'Content-Type: application/json; charset=gb2312',
'Content-Length: ' . strlen($this->data)
));
 
return $ch;
}
 
 
//(2).利用会话句柄,发送Json,返回状态和文本
public function postjson($ch){
 
//开启缓存区
ob_start();
 
//执行CURL会话
curl_exec($ch);
 
//获取缓冲区的内容
$return_content=ob_get_contents();
 
//关闭缓冲区
ob_end_clean();
 
 
//获取资源句柄的信息
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
 
 
return array($return_code, $return_content);   
}
 
 
 
 
}
 
/*类结束*/
 
 
 
 
 
 
 
 
 
//设置参数
 
$url  = "http://weixin.xajlnkyjy.com/lswyjishu/renshi.php/Bcdingding/baocan";  /*目标URL*/
 
$info = array
(
         shangwu=>'吃',
         xiawu=>'吃',
         jobnumber=>'0209060334387389791',
         ename=>'高久峰',
         deptname=>'安康项目组'
 
        );
 
 
$data = json_encode($info);/*Json数据*/   
 
 
 
 
 
 
//实例化
$Baocan=new AutoBaocan($url,$data);
 
//执行发送json
$result=$Baocan->postjson($Baocan->newcurl());
 
 
//生成日志
file_put_contents('log.txt',$result[0]."--".$result[1]."\r\n",FILE_APPEND);
 
?>

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/21.html

分享给朋友:

“PHP使用post发送Json数据” 的相关文章

php限制方法返回值类型

php限制方法返回值类型

php7新增的特性(1).强制限制只能返回一种类型<?php class task { } //must return an integer function add(): int {    &nb...

php定义常量数组

php定义常量数组

<?php //php7+ define('CONFIG', [     'MYSQL' => '127.0.0.1',     ...

php端口复用,php socket端口复用

php端口复用,php socket端口复用

第一次听说端口复用是在mixphp最新版本中发现的,mixphp启动监听9501端口,现在作者说可以多开几个进程来执行mixphp,我心里想了下再启动不是会端口冲突嘛,但是却没有问题,于是下载mixphp的源码解读,原来是启动http服务器使用new Co\Http\Server('0.0....

在MyISAM引擎中使用事务会怎样

在MyISAM引擎中使用事务会怎样

众所周知MyISAM引擎不支持事务,但是我只是知道不支持事务,并未测试具体的表现是什么,测试代码如下:try {     //开启事务     Db::startTrans();    &...

windows安装php event扩展问题

windows安装php event扩展问题

php event扩展在windows中依赖于php_sockets扩展,因此在php.ini中必须先加载php sockets扩展,如下。extension=sockets extension=event...

php生成器yield from详解

php生成器yield from详解

PHP7中,通过生成器委托(yield from),可以将其他生成器、可迭代的对象、数组委托给外层生成器。外层的生成器会先顺序 yield 委托出来的值,然后继续 yield 本身中定义的值。同时yield from也能获取到生成器的返回值...