Php method for submitting and acquiring data using curl analog get and post to micro-communication interface _php instance

Source: Internet
Author: User
Tags commit

This example describes the way PHP submits and obtains data using the curl analog get and post to the micro-communication interface. Share to everyone for your reference, specific as follows:

PHP Curl function can imitate the user to do some operations, such as we can imitate the user submits the data also can imitate the user to carry on the website to visit, below we will introduce uses the curl simulation to carry on the micro-communication interface's get and the post example, the example very simple two:

Get commit FETCH data

/**
* @desc get access_token
* @return String access_token
/function Getaccesstoken () {
  $AppId = ' 1232assad13213123 ';
  $AppSecret = ' 2312312321adss3123213 ';
  $GETURL = ' https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= '. $AppId. ' &secret= '. $AppSecret;
  $ch = Curl_init ();
  curl_setopt ($ch, Curlopt_url, $GETURL);
  curl_setopt ($ch, Curlopt_header, 0);
  curl_setopt ($ch, Curlopt_returntransfer, 1);
  curl_setopt ($ch, Curl_sslversion_ssl, 2);
  curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);
  curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE);
  $data = curl_exec ($ch);
  $response = Json_decode ($data);
  return $response->access_token;
}

Post commit fetch data

/** * @desc achieve weather content reply/Public function testweixin () {$access _token = $this->getaccess
Token ();
$CUSTOMMESSAGESENDURL = ' https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token= '. $access _token; $description = ' Details of today's weather (obtained from a third party).
';
$url = ' http://weather.com/';
$picurl = ' http://weather.com/'; $POSTDATAARR = Array (' Touser ' => ' OPENID ', ' msgtype ' => ' news ', ' News ' =>array (' articles ' =>array ') (' title ' =& gt; '
Day of the weather ', ' description ' => $description, ' url ' => $url, ' Picurl ' => $picurl,),);
$postJosnData = Json_encode ($POSTDATAARR);
$ch = Curl_init ($CUSTOMMESSAGESENDURL);
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_postfields, $postJosnData);
curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);
curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE);
$data = curl_exec ($ch);
Var_dump ($data); }

The example is relatively simple and there is no good detailed analysis, we can copy to achieve the function we want.

For more information on PHP related content readers can view the site topics: "PHP micro-credit Development Skills summary", "PHP coding and transcoding Operation skills Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP string (String) Usage Summary", "php+ MySQL Database operations Introduction tutorial and PHP Common database operation Skills Summary

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.