コード例 #1
0
ファイル: catalog.php プロジェクト: gabzon/experiensa
 public static function get_catalog()
 {
     $code = Helpers::getActiveLanguageCode();
     if (!$code) {
         $lang_req = "";
     } else {
         $lang_req = '?lang=' . $code;
     }
     $api_response = [];
     //Agency Catalog
     $agency_api_url = get_site_url() . '/wp-json/wp/v2/voyage';
     //        echo " el agency api url es ".$agency_api_url;
     if (function_exists('curl_version')) {
         //Using Curl
         //  Initiate curl
         $ch = curl_init();
         // Disable SSL verification
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
         // Will return the response, if false it print the response
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         // Set the url
         $real_url = $agency_api_url . $lang_req;
         //            echo "el real url propio es ".$real_url;
         curl_setopt($ch, CURLOPT_URL, $real_url);
         // Execute
         $agency_response = curl_exec($ch);
         if (!$agency_response) {
             curl_setopt($ch, CURLOPT_URL, $agency_api_url);
             $agency_response = curl_exec($ch);
         }
         // Closing
         curl_close($ch);
     } else {
         if (ini_get('allow_url_fopen')) {
             $agency_response = @file_get_contents($agency_api_url . $lang_req);
             if (!$agency_response) {
                 $agency_response = @file_get_contents($agency_api_url);
             }
         } else {
             $agency_response = "";
         }
     }
     //        echo "<pre>";
     //        print_r($agency_response);
     //        echo "</pre>";
     $agency_response = json_decode($agency_response);
     $api_response[] = $agency_response;
     //Partners Catalog
     $partners = Partners::partnerApiList();
     if (!empty($partners) && Helpers::check_internet_connection()) {
         for ($i = 0; $i < count($partners); $i++) {
             // Check if  $partners[$i]['website'] dont have '/' on last char
             $api_url = $partners[$i]['website'];
             if (substr($partners[$i]['website'], -1) != '/') {
                 $api_url .= '/';
             }
             $api_url .= 'wp-json/wp/v2/voyage';
             //                echo "<br>entro a un partner ".$api_url;
             //Check if $api_url is a valid url
             if (!(filter_var($api_url, FILTER_VALIDATE_URL) === FALSE)) {
                 //                    echo "<br>entro aqui ".$api_url;
                 $file_headers = @get_headers($api_url);
                 //                    echo "<br>los header de ".$api_url;
                 //                    echo "<pre>";
                 //                    print_r($file_headers);
                 //                    echo "</pre>";
                 //check if url have response HTTP/1.1 200 OK
                 if (!empty($file_headers) && strpos($file_headers[0], 'OK') !== FALSE) {
                     //Using Curl
                     if (function_exists('curl_version')) {
                         //  Initiate curl
                         $ch = curl_init();
                         // Disable SSL verification
                         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                         // Will return the response, if false it print the response
                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                         // Set the url
                         $real_url = $api_url . $lang_req;
                         //                            echo "<br> real url ".$real_url;
                         curl_setopt($ch, CURLOPT_URL, $real_url);
                         // Execute
                         $partner_response = curl_exec($ch);
                         if (!$partner_response) {
                             //                                echo "<br> api url ".$api_url;
                             curl_setopt($ch, CURLOPT_URL, $api_url);
                             $partner_response = curl_exec($ch);
                         }
                         // Closing
                         curl_close($ch);
                     } else {
                         if (ini_get('allow_url_fopen')) {
                             $partner_response = @file_get_contents($api_url . $lang_req);
                             if (!$partner_response) {
                                 $partner_response = @file_get_contents($api_url);
                             }
                         } else {
                             $partner_response = "";
                         }
                     }
                     $partner_response = json_decode($partner_response);
                     $api_response[] = $partner_response;
                 }
             }
         }
     }
     //        echo "<pre>";
     //        print_r($api_response);
     //        echo "</pre>";
     $voyages = array();
     for ($i = 0; $i < count($api_response); $i++) {
         for ($j = 0; isset($api_response[$i]) && $j < count($api_response[$i]); $j++) {
             $voyage = $api_response[$i][$j];
             $tab = ['id' => $voyage->id, 'title' => $voyage->title->rendered, 'excerpt' => $voyage->excerpt->rendered, 'cover_image' => $voyage->cover_image, 'currency' => $voyage->currency, 'price' => $voyage->price, 'itinerary' => $voyage->itinerary, 'duration' => $voyage->duration, 'country' => isset($voyage->country) ? $voyage->country : "", 'location' => isset($voyage->location) ? $voyage->location : "", 'theme' => isset($voyage->theme) ? $voyage->theme : "", 'api_link' => $voyage->link, 'website' => $voyage->website, 'website_name' => $voyage->website_name];
             $voyages[] = $tab;
         }
     }
     return $voyages;
     // Restore original Post Data
     wp_reset_postdata();
 }