Curl and file_get_contents implement a simulated POST request

Source: Internet
Author: User
Tags how to use curl

Before the interview, the interviewer asked me if there is a cross-domain problem, but I am not sure, now can be sure to say no.

Using the PHP curl and File_get_contents methods to implement the backend cross-domain respectively. The scenario in this article is also implemented under TP5.

One, using curl to achieve

1) front-end code, which is a page that accesses the path to Http://app.write.com/thinkphp/public/index.php/index/index/curl, module index, controller index, Method curl

1<! DOCTYPE html>2"en">34<meta charset="UTF-8">5<title> Test curl</title>67<body>8     9<div id="Test">Ten          One</div> A<script type="Text/javascript"> -         //the use of native XML -     varOajax =NewXMLHttpRequest (); theOajax.open ('GET',"/thinkphp/public/index.php/index/index/curlapi");//The front end is a GET request URL and does not affect the backend -Oajax.onreadystatechange =function () { -         if(Oajax.readystate = =4) { -             if(Oajax.status >= $&& Oajax.status < -|| Oajax.status = =304) { + Console.log (oajax.responsetext); -                varData=Json.parse (oajax.responsetext); +document.getElementById ("Test"). innerhtml=data.elist[0]. Code; A}Else { at Console.log (oajax.status); -             } -         } -     }; - oajax.send (); -</script> in</body> -

2) Interface Code

1  //How to use curl to implement a package2Public Function Docurlpostrequest ($url, $requestString, $timeout =5){3          if($url = ="'|| $requestString = ="'|| $timeout <=0){4                return false;5           }6$con = Curl_init ((string) $url);//Initializes a curl session. 7         //SET options for a session8curl_setopt ($con, Curlopt_header,false);9 curl_setopt ($con, Curlopt_postfields, $requestString);Tencurl_setopt ($con, Curlopt_post,true); Onecurl_setopt ($con, Curlopt_returntransfer,true); Acurl_setopt ($con, Curlopt_timeout, (int) $timeout); -         //performs a curl session.  -$data =curl_exec ($con);  the         //Close a session - curl_close ($con);  -          -         return$data; +      } -       Publicfunction Curlapi () { +         //examples of custom parameters A$URLARR =[ at             "PageNo"=1, -             "pagesize"= $, -             "Condstr"="Social Big classroom: 0" -          ]; -$post _string =http_build_query ($URLARR); -         in$url ="cross-domain full address"; -         return$ This-docurlpostrequest ($url, $post _string); to         ///return "common"; +}

Second, using file_get_contents to realize

The front-end code is consistent and the back-end encapsulates the method

1    Publicfunction Filegetcontent ($SURL, $data) {2$aContext = [3             'http'= [4                 'Method'='POST',5                 'Header'='content-type:application/x-www-form-urlencoded',6                 'content'=$data7               ]8           ]; 9   Ten$cxContext = Stream_context_create ($aContext);//Create and return a text data stream and apply various options One$d = file_get_contents ($SURL,false, $cxContext);  A        return$d; -}

The above Docurlpostrequest method can be replaced by filegetcontent.

Of course, you can also put two methods in the common.php to use directly.

This article concludes.

Curl and file_get_contents implement a simulated POST request

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.