php版微信小店调用api示例代码

yipeiwu_com5年前PHP代码库

本文实例讲述了php版微信小店调用api的方法。分享给大家供大家参考,具体如下:

刚开始调用微信小店api的时候,可能大家会遇到问题。系统总是提示system error,归根结底还是发送的参数不正确。

下面给出几个调用例子:

例子写得不全。

<?php
function cUrlRequest($url,$data = null){
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  if (!empty($data)){
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  }
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  $output = curl_exec($curl);
  curl_close($curl);
  return $output;
}
//获取所有商品
function gStateProduct($state = 0,$token){
   //https://api.weixin.qq.com/merchant/getbystatus?access_token=ACCESS_TOKEN
   //{"status": 0}
  // $AccessToken = "xxxxxxxx";
   $url = "https://api.weixin.qq.com/merchant/getbystatus?access_token=".$token;
   //print_r($this->AccessToken);
   $ResData = cUrlRequest($url,'{"status": '.$state.'}');  //特别注意这里,这个是json格式的。
   echo "<pre>";
   print_r( json_decode($ResData) );
}
//获得所有分组
function getAllCategory($state = 0,$token)
{
    $url = "https://api.weixin.qq.com/merchant/group/getall?access_token=".$token;
    $ResData = cUrlRequest($url,'{"status": '.$state.'}');
    echo "<pre>";
    print_r( json_decode($ResData) );
}
//根据分组id来获得商品信息
function getProductByGroudid($state = 0,$token)
{
  $url = "https://api.weixin.qq.com/merchant/group/getbyid?access_token=".$token;
  $ResData = cUrlRequest($url,'{"group_id": '.$state.'}');
  echo "<pre>";
   print_r( json_decode($ResData) );
}
//获取accesstoken
function getAccessToken()
{
    $appid = "your appid";
    $appsecret = "your appsecret";
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
    $result = cUrlRequest($url);
    $jsoninfo = json_decode($result, true);
    $access_token = $jsoninfo["access_token"];
    return $access_token;
}
//根据product_id来获取单个商品
function getOneProductByid($state = 0,$token)
{
  $url="https://api.weixin.qq.com/merchant/get?access_token=".$token;
  $ResData = cUrlRequest($url,'{"product_id": "'.$state.'""}');
  echo "<pre>";
   print_r( json_decode($ResData) );
}
$AccessToken=getAccessToken();
//gStateProduct(1,$AccessToken);
//getAllCategory(1,$AccessToken);
//getProductByGroudid(207061749,$AccessToken);
getOneProductByid("pf82VwN45zr8eOlur5mAiSTjg8WU",$AccessToken);

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP微信开发技巧汇总》、《PHP编码与转码操作技巧汇总》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

Zend Framework中的简单工厂模式 图文

Zend Framework中的简单工厂模式 图文

前段时间用来ZF,把他当黑盒感觉不是很好,一直有看其源码的冲动,但是。。。如果一点一点点看的话,挑战确实有些大了。某天又然后想到好久没复习设计模式了。综合一下,复习一个设计模式之后在ZF...

PHP动态生成指定大小随机图片的方法

本文实例讲述了PHP动态生成指定大小随机图片的方法。分享给大家供大家参考,具体如下: <?php $image_width = 100; $image_height =...

解析php中两种缩放图片的函数,为图片添加水印

有两种改变图像大小的方法.(1):ImageCopyResized() 函数在所有GD版本中有效,但其缩放图像的算法比较粗糙.(2):ImageCopyResampled(),其像素插值...

phpmail类发送邮件函数代码

有了phpmail这个类,你就不用愁了。这是个外国人写的一个类,我们就只管“拿来主义”了。下面是基于这个类里面的send()方法写的一个函数: 复制代码 代码如下: function s...

php基础知识:类与对象(2) 自动加载对象

自动加载对象:    很多开发者写面向对象的应用程序时对每个类的定义建立一个 PHP 源文件。一个很大的烦恼是不得不在每个脚本(每个类一...