PHP (Curl request) test interface case

Source: Internet
Author: User

Request the Test interface as follows:

 $data = [ " mobilephone  "  => " 123   ", "  password   " =>"   124   " ]; $result  = Curlrequest ($APIURL, $data);  if  ($result [ " statuscode   ' ] = = 200   else  {echo Json_encode ($result);}  

The Curl Request URL method is as follows:

/** * @param string $url Request address * @param string $data Request data * @param string $method Request way * @return Array one-dimensional array*/function Curlrequest ($url, $data="', $method ='POST') {$ch= Curl_init ();//Initialize the curl handlecurl_setopt ($ch, Curlopt_url, $url);//set the requested URLcurl_setopt ($ch, Curlopt_returntransfer,1);//set to True to convert the curl_exec () result to a string, while S is not a direct outputcurl_setopt ($ch, Curlopt_customrequest, $method);//Set Request Modecurl_setopt ($ch, Curlopt_httpheader,array ("x-http-method-override: $method"));//Set HTTP header informationcurl_setopt ($ch, Curlopt_postfields, $data);//set the committed string$document = curl_exec ($ch);//Perform predefined Curl$statusCode = Curl_getinfo ($ch, Curlinfo_http_code);//get HTTP request status Code ~curl_close ($ch); $document= Json_decode (Removebom ($document),true); $document ['StatusCode'] =$statusCode; return$document;}

If garbled characters are found in the returned JSON results, the BOM needs to be removed as follows:

/* * @param string $str String * @return string to remove after BOM */ " ) {    if0,3) = = Pack ("CCC" ,0xef,0xbb,0xbf)) {        3);    }     return $str;}

PHP (Curl request) test interface case

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.