Was this page helpful?

AGI: IVR開發-整合 HTTP GET

    說明

    開發 IVR 系統與 3rd party 作整合時,通常會透過 MySQL 來作資料的傳遞,不過對於異質平台系統的整合,更常使用的方式是 HTTP GET 方式,也就是說,Asterisk 透過 HTTP URL 來取得需要的資料數值。

    技巧:

    如何在 Shell mode 測試 Hit URL 的回傳值:

    lynx -dump http://your.url.address/.....
    
    範例一:php 模組 Curl
    /*======================================================================
    Get url content and response headers (given a url, follows all redirections on it and returned content and response headers of final url)
    
    @return    array[0]    content
               array[1]    array of response headers
    ========================================================================*/
    
    function get_url( $url,  $javascript_loop = 0, $timeout = 5 )
    {
        $url = str_replace( "&", "&", urldecode(trim($url)) );
    
        $cookie = tempnam ("/tmp", "CURLCOOKIE");
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $ch, CURLOPT_ENCODING, "" );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );    # required for https urls
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
        curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
        curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
        $content = curl_exec( $ch );
        $response = curl_getinfo( $ch );
        curl_close ( $ch );
    
        if ($response['http_code'] == 301 || $response['http_code'] == 302)
        {
            ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
    
            if ( $headers = get_headers($response['url']) )
            {
                foreach( $headers as $value )
                {
                    if ( substr( strtolower($value), 0, 9 ) == "location:" )
                        return get_url( trim( substr( $value, 9, strlen($value) ) ) );
                }
            }
        }
    
        if (    ( preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value) ) &&
                $javascript_loop < 5
        )
        {
            return get_url( $value[1], $javascript_loop+1 );
        }
        else
        {
            return array( $content, $response );
        }
    }
    
    $info = get_url($HTTP_URL.$CODE);
    $date_full = "$info[0]"; // date format YYYY-MM-DD
    $date_now = date("Y-m-d", strtotime("today"));
     
    範例二:php 函數 fopen( )

    http://nerdvittles.com/index.php?p=160

    // e.g: ZIP code is 90210
    $query = "http://www.srh.noaa.gov/port/port_zc.php?inputstring=$zip" ;
      
    $fd = fopen($query, "r");
    if (!$fd) {
     echo "<p>Unable to open web connection. \n";
     exit;
    }
    $value = "";
    while(!feof($fd)){
            $value .= fread($fd, 4096);
    }
    fclose($fd);
    
    if ($debug) :
      fputs($stdlog, "\nQuery Results: " . $value . "\n\n" );
    endif ;
    
    
    //$thetext = "National Weather Service" ;
    //$start= strpos($value, $thetext);
    //$newvalue=substr($value, $start);
    
    //$thetext = "<div align=\"center\">" ;
    $thetext = "Current Local Conditions";
    $start= strpos($value, $thetext);
    //$newvalue="This National Weather Service update provided for " . substr($newvalue, $start+26);
    $value = substr($value,$start);
    $thetext = "<br>" ;
    $start= strpos($value, $thetext);
    $cityupdate="This National Weather Service update provided for ". $city . ". ";
    $newvalue="Current local conditions at " . substr($value, $start);
    
    $finish= strpos($newvalue, "<hr>"); 
    $newvalue=substr($newvalue, 0, $finish);
    
    // new code to delete Latitude, Longitude, and Elevation info from report
    $start = strpos($newvalue,"<br>") ;
    $start2 = strpos($newvalue,"<br>",$start+1) ;
    $start3 = strpos($newvalue,"<br>",$start2+1) ;
    $newvalue = substr($newvalue,0,$start2) . " " . substr($newvalue,$start3) ;
    // new code ends here
    
    $thetext = "Last Update:" ;
    $start = strpos($newvalue, $thetext);
    $thedate = substr($newvalue,$start+13,8) ;
    
    $themo = substr($thedate,0,2);
    $theda = substr($thedate,3,2);
    switch ($themo) {
     case "01":
      $themo="January ";
      break;
     case "02":
      $themo="February ";
      break;
     case "03":
      $themo="March ";
      break;
     case "04":
      $themo="April ";
      break;
     case "05":
      $themo="May ";
      break;
     case "06":
      $themo="June ";
      break;
     case "07":
      $themo="July ";
      break;
     case "08":
      $themo="August ";
      break;
     case "09":
      $themo="September ";
      break;
     case "10":
      $themo="October ";
      break;
     case "11":
      $themo="November ";
      break;
     case "12":
      $themo="December ";
      break;
    }
    
    if (substr($theda,0,1)=="0") :
     $theda=substr($theda,1,1) ;
    endif ;
    Was this page helpful?
    標籤 (Edit tags)
    • No tags
    您必須 登入 才能發佈評論。
    Powered by MindTouch Core