PHP calls the Apsara API to obtain weather forecasts and send text messages

Source: Internet
Author: User
PHP calls the Apsara API to obtain weather forecasts and send text messages
  • ##########
    # Jiam 2012/12/5
    # 1113160783@qq.com
    # Automatic sending of Apsara stack
    # The Apsara stack interface is involved in the middle
    # Weather forecast API
    # And city code
    ##########
    #101180101 is the Zhengzhou city code
    #101180301 is the Xinxiang city code
    $ Url = 'http: // m.weather.com.cn/data/101180101.html ';
    $ Xinxiang = 'http: // m.weather.com.cn/data/101180301.html ';
    $ W = curl ($ url );
    $ Weather = json_decode ($ w );
    $ Weatherinfo = object_to_array ($ weather );
    $ Info = $ weatherinfo ['weatherinfo'];
    $ Str = $ info ['date _ Y']. ','. $ info ['Week '].'. '. $ Info ['city']. 'Today's temperature :'. $ info ['temp1']. ', weather :'. $ info ['weather 1']. 'you '. $ info ['wind1 ']. ', wind power :'. $ info ['fx1 ']. ', recommended dressing :'. $ info ['index _ d']. $ info ['index48 _ d'];
     
    $ Feixin = 'http: // wjima.a173.cnaaa4.com/feixin/str.php ';
    $ PostArray ['username'] = 'mobile phone number'; // Apsara stack account
    $ PostArray ['password'] = 'password'; // Apsara stack password
    $ PostArray ['content'] = $ str; // send content
    $ PostArray ['Other'] = 'receiver's mobile phone number'; // The receiver's mobile phone number. If this parameter is not set, it is sent to itself by default.
     
    $ Send = curl ($ feixin, $ postArray );
     
    Print_r ($ send); die ();
     


    Function curl ($ url, $ postFields = null)
    {
    $ Ch = curl_init ();
    Curl_setopt ($ ch, CURLOPT_URL, $ url );
    Curl_setopt ($ ch, CURLOPT_FAILONERROR, false );
    Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
    // Https request
    If (strlen ($ url)> 5 & strtolower (substr ($ url, 0, 5) = "https "){
    Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false );
    Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false );
    }
     
    If (is_array ($ postFields) & 0 <count ($ postFields ))
    {
    $ PostBodyString = "";
    $ PostMultipart = false;
    Foreach ($ postFields as $ k => $ v)
    {
    If ("@"! = Substr ($ v, 0, 1) // You can check whether a file is uploaded.
    {
    $ PostBodyString. = "$ k =". urlencode ($ v )."&";
    }
    Else // multipart/form-data for file upload; otherwise, www-form-urlencoded
    {
    $ PostMultipart = true;
    }
    }
    Unset ($ k, $ v );
    Curl_setopt ($ ch, CURLOPT_POST, true );
    If ($ postMultipart)
    {
    Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ postFields );
    }
    Else
    {
    Curl_setopt ($ ch, CURLOPT_POSTFIELDS, substr ($ postBodyString, 0,-1 ));
    }
    }
    $ Reponse = curl_exec ($ ch );
     
    If (curl_errno ($ ch ))
    {
    Throw new Exception (curl_error ($ ch), 0 );
    }
    Else
    {
    $ HttpStatusCode = curl_getinfo ($ ch, CURLINFO_HTTP_CODE );
    If (200! ==$ HttpStatusCode)
    {
    Throw new Exception ($ reponse, $ httpStatusCode );
    }
    }
    Curl_close ($ ch );
    Return $ reponse;
    }


    Function object_to_array ($ obj)
    {
    $ _ Arr = is_object ($ obj )? Get_object_vars ($ obj): $ obj;
    Foreach ($ _ arr as $ key => $ val)
    {
    $ Val = (is_array ($ val) | is_object ($ val ))? Object_to_array ($ val): $ val;
    $ Arr [$ key] = $ val;
    }
    Return $ arr;
    }
    ?>

  • 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.