PHP calls interface methods with curl, get and post two ways

Source: Internet
Author: User
<span id="Label3"></p><p><p>The first is the client execution method apimodel.php:</p></p><pre><?<span style="color: #000000;"><span style="color: #000000;">PHP</span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;">* <span style="color: #008000;">* analog post for URL request * @param string $url * @param array $post _data</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>Request_post (<span style="color: #800080;"><span style="color: #800080;">$url</span></span>= ",<span style="color: #800080;"><span style="color: #800080;">$ispost</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span>,<span style="color: #800080;"><span style="color: #800080;">$post _data</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">Array</span></span><span style="color: #000000;"><span style="color: #000000;">()) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(<span style="color: #0000ff;"><span style="color: #0000ff;">Empty</span></span>(<span style="color: #800080;"><span style="color: #800080;">$url</span></span>) ||<span style="color: #0000ff;"><span style="color: #0000ff;">Empty</span></span>(<span style="color: #800080;"><span style="color: #800080;">$post _data</span></span><span style="color: #000000;"><span style="color: #000000;">)) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #800080;"><span style="color: #800080;">$o</span></span>= ""<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">foreach</span></span>(<span style="color: #800080;"><span style="color: #800080;">$post _data</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> as</span> <span style="color: #800080;"><span style="color: #800080;">$k</span></span>=<span style="color: #800080;"><span style="color: #800080;">$v</span></span><span style="color: #000000;"><span style="color: #000000;"> ) { </span></span><span style="color: #800080;"><span style="color: #800080;">$o</span></span>.= "<span style="color: #800080;"><span style="color: #800080;">$k</span></span>=" .<span style="color: #008080;"><span style="color: #008080;">UrlEncode</span></span>(<span style="color: #800080;"><span style="color: #800080;">$v</span></span>). "&"<span style="color: #000000;"><span style="color: #000000;"> ; } </span></span><span style="color: #800080;"><span style="color: #800080;">$post _data</span></span>=<span style="color: #008080;"><span style="color: #008080;">substr</span></span>(<span style="color: #800080;"><span style="color: #800080;">$o</span></span>, 0,-1<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #800080;"><span style="color: #800080;">$key</span></span>=<span style="color: #008080;"><span style="color: #008080;">MD5</span></span>(<span style="color: #008080;"><span style="color: #008080;">Base64_encode</span></span>(<span style="color: #800080;"><span style="color: #800080;">$post _data</span></span><span style="color: #000000;"><span style="color: #000000;">)); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(<span style="color: #800080;"><span style="color: #800080;">$ispost</span></span><span style="color: #000000;"><span style="color: #000000;">){ </span></span><span style="color: #800080;"><span style="color: #800080;">$url</span></span>=<span style="color: #800080;"><span style="color: #800080;">$url</span></span><span style="color: #000000;"><span style="color: #000000;">; }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{ </span></span><span style="color: #800080;"><span style="color: #800080;">$url</span></span>=<span style="color: #800080;"><span style="color: #800080;">$url</span></span>.‘?‘.<span style="color: #800080;"><span style="color: #800080;">$post _data</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #800080;"><span style="color: #800080;">$curlPost</span></span>= ' key= '.<span style="color: #800080;"><span style="color: #800080;">$key</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008080;"><span style="color: #008080;">Header</span></span>("content-type:text/html; Charset=utf-8 "<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #800080;"><span style="color: #800080;">$ch</span></span>= Curl_init ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Initialize Curl</span></span>curl_setopt (<span style="color: #800080;"><span style="color: #800080;">$ch</span></span>, curlopt_url,<span style="color: #800080;"><span style="color: #800080;">$url</span></span>);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">crawl specified Web page</span></span>curl_setopt (<span style="color: #800080;"><span style="color: #800080;">$ch</span></span>, curlopt_header, 0);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set Header</span></span>curl_setopt (<span style="color: #800080;"><span style="color: #800080;">$ch</span></span>, curlopt_returntransfer, 1);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">request result As string and output to screen</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(<span style="color: #800080;"><span style="color: #800080;">$ispost</span></span><span style="color: #000000;"><span style="color: #000000;">) {curl_setopt (</span></span><span style="color: #800080;"><span style="color: #800080;">$ch</span></span>, curlopt_post, 1);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Post Submission Method</span></span>curl_setopt (<span style="color: #800080;"><span style="color: #800080;">$ch</span></span>, curlopt_postfields,<span style="color: #800080;"><span style="color: #800080;">$curlPost</span></span><span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #800080;"><span style="color: #800080;">$data</span></span>= Curl_exec (<span style="color: #800080;"><span style="color: #800080;">$ch</span></span>);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Run Curl</span></span>Curl_close (<span style="color: #800080;"><span style="color: #800080;">$ch</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #800080;"><span style="color: #800080;">$data</span></span><span style="color: #000000;"><span style="color: #000000;">; } </span></span>?></pre><p><p>The client calls the method, where you can configure the basic information api.php:</p></p><pre><?<span style="color: #000000;"><span style="color: #000000;">PHP</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">require</span></span>' Apimodel.php '<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">testaction () {</span></span><span style="color: #800080;"><span style="color: #800080;">$url</span></span>= ' interface address '<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #800080;"><span style="color: #800080;">$post _data</span></span>[' AppID '] = ' 10 '<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #800080;"><span style="color: #800080;">$post _data</span></span>[' appkey '] = ' Cmbohpffxvr03nipkkqxaaa1vf5no4nq '<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #800080;"><span style="color: #800080;">$post _data</span></span>[' member_name '] = ' zsjs124 '<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #800080;"><span style="color: #800080;">$post _data</span></span>[' password '] = ' 123456 '<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #800080;"><span style="color: #800080;">$post _data</span></span>[' email '] = ' [email protected] '<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">$post _data = array ();</span></span> <span style="color: #800080;"><span style="color: #800080;">$res</span></span>= Request_post (<span style="color: #800080;"><span style="color: #800080;">$url</span></span>,<span style="color: #800080;"><span style="color: #800080;">$ispost</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span>,<span style="color: #800080;"><span style="color: #800080;">$post _data</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008080;"><span style="color: #008080;">Print_r</span></span>(<span style="color: #800080;"><span style="color: #800080;">$res</span></span><span style="color: #000000;"><span style="color: #000000;">); }testaction ();</span></span>?></pre><p><p>The Server's interface function test.php:</p></p><pre><?<span style="color: #000000;"><span style="color: #000000;">PHP</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">Serverapi () {</span></span><span style="color: #800080;"><span style="color: #800080;">$key</span></span>= ' 57173d6ad842d807443ee0db91fed323 '<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(<span style="color: #800080;"><span style="color: #800080;">$_get</span></span>&&<span style="color: #800080;"><span style="color: #800080;">$_get</span></span>[' appkey ']== ' Cmbohpffxvr03nipkkqxaaa1vf5no4nq ' | |<span style="color: #800080;"><span style="color: #800080;">$_post</span></span>&&<span style="color: #800080;"><span style="color: #800080;">$_post</span></span>[' Key ']===<span style="color: #800080;"><span style="color: #800080;">$key</span></span><span style="color: #000000;"><span style="color: #000000;">){ </span></span><span style="color: #800080;"><span style="color: #800080;">$arr</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">Array</span></span>(' name ' = ' Huanglu ', ' password ' = ' 123456 '<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Echo</span></span>Json_encode (<span style="color: #800080;"><span style="color: #800080;">$arr</span></span><span style="color: #000000;"><span style="color: #000000;">); }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{ </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Exit</span></span>(' illegal access! ‘<span style="color: #000000;"><span style="color: #000000;">); }}serverapi ();</span></span>?></pre><p><p></p></p><p><p>PHP calls interface methods with curl, get and post two ways</p></p></span>

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.