Home > Backend Development > PHP Tutorial > PHP Feature 01: cURL implements get and post, and performs data interaction between URLs;

PHP Feature 01: cURL implements get and post, and performs data interaction between URLs;

WBOY
Release: 2016-07-28 08:26:37
Original
1109 people have browsed it

One background

The development interface of WeChat public account usually uses http request, selects http/https protocol, supports curl command to get or post data;

two php function cURL function

01 Initialize cURL Session, return handle;
resource curl_init ([ string $url = NULL ] )
02 Set a cURL transfer option;
bool curl_setopt ( resource $ch , int $option , mixed $value )
ex: curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); <br> //Return the information obtained by curl_exec() in the form of a file stream instead of outputting it directly.
03 Execute cURL session
mixed curl_exec ( resource $ch )mixed curl_exec ( resource $ch )

04 Close a cURL session and release all resources. The cURL handle ch will also be released void curl_close ( resource $ch )

void curl_close ( resource $ch )

🎜Three PHP Get and Post implementation🎜

Get // Scenario 1: Upload picture materials to WeChat server
`ch=curli nit( );access_token; //Interface call credentials; need to be obtained actively;
typ e= image";url = "http://file.api.weixin.qq.com/cgi-bin/media/ upload?access_token=access_token&type=type”;
$filepath = dirname(FILE)."test.jpg";
filedata =a rray("media"= >" @".filepath);
curl_setopt(ch,CURLOP TU RL,url); // Required
curl_setopt(ch,CURLOP TR ETURNTRANSFER ,1) " curl setopt(ch, CURLOPT_POST,1); // post data curl_setopt(ch,CURLOP T
P // post variable curl_setopt(ch,CURLOP TH EADER,0);
output = curl_exec( ch) ;curl_close(ch);va rd ump(output); // Decompress json data packet; outputa rray= jsondecode (output,true ); d
=output_array[" media_id”]; echo $media_id; "Post // Download picture material (part CODE) from WeChat server access t


oken;// pick up Add with with Certificate; Need WantMasterMove GetGet ; media_id; // Obtained when uploading picture material; $url = "http://file.api.weixin.qq.com/ cgi-bin/media/get?access_token=$access_token&media_id=$media_id"; $ch =curl_init(); curl_setopt($ch,CURLOPT_URL,$URL); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); $output = curl_exec($ch); curl_close($ch); ').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); }); The above introduces PHP feature 01: cURL implements get and post, and performs data interaction between URLs; including aspects of the content, I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template