PHP some practical self-made methods

Source: Internet
Author: User
Tags error handling

JSON garbled to Chinese

function decodeUnicode($str){
  return preg_replace_callback(‘/\\\\u([0-9a-f]{4})/i‘,
    create_function(
      ‘$matches‘,
      ‘return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");‘
    ),
    $str);
}

Curl Crawler

Function _grab($curl,$postInfo=‘‘,$cookie=‘‘,$referer=‘‘,$userAgent=‘‘){
     $ch = curl_init();
     Curl_setopt($ch, CURLOPT_URL, $curl);
     / / No output header
     Curl_setopt($ch, CURLOPT_HEADER, 0);
     / / Returns the obtained information as a string, not directly output
     Curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     / / If it is a https link, do not verify the certificate
     If(preg_match(‘/https/i‘, $curl)){
         Curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     }
     //POST
     If($postInfo){
        Curl_setopt($ch,CURLOPT_POST,1);
        Curl_setopt($ch,CURLOPT_POSTFIELDS,$postInfo);
     }
     //Add a cookie
     If($cookie){
         Curl_setopt($ch,CURLOPT_COOKIE,$cookie);
     }
     / / Analog to the road
     If($referer){
         Curl_setopt($ch, CURLOPT_REFERER, $referer);
     }
     / / Simulation environment
     If($userAgent){
         Curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
     }
     //carried out
     $content = curl_exec($ch);
     / / Error handling
     If ($content === false) {
       Return "Network request error:" . curl_error($ch);
       Exit();
     }
     Return $content;
}


PHP some practical self-made methods

Related Article

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.