make multiple API calls and use $object in another function
-
Hello ??
I’m trying to call an API and get the project and combine another call with the first one.
In my first function I’m making a API call to get a list with projects. Each project has got a name, id, etc.
In my second function I’m making a different API call to get the campaign id for a specific project.When using the shortcode in my page, I can see all the projects with the name of each project.
But I cannot seem to get the specific campaign id for each project?I need to use the project id from the first function and add it to the url in the second function,
and then somehow make the call for every project and show it on my page.Maybe this is not the right way to do all of this, but hope someone can help me to get this to work.
// START CLASS class My_First_class_Public { // START OF PROJECT_LIST FUNCTION() public function project_list($CID) { // Start output buffering so get_template_part doesn't output to the page ob_start(); $api_key = get_option( 'my_personal_api_key' ); if ( empty( $api_key ) ) { return; } $request = wp_remote_get('https://api.domain.com/projectslist.svc?api_key='.$api_key); // URL TO GET THE PROJECT LIST // OUTPUT JSON if( is_wp_error( $request ) ) { return false; // Bail early } $body = wp_remote_retrieve_body( $request ); $data = json_decode( $body ); if( ! empty( $data ) ) { /* Show specific results in html/css */ foreach( $data as $project ) { $projectname = ($project->name); $projectid = ($project->id); $this->PID = $projectid; echo '<div class="col medium-4 small-12 large-4"><div class="col-inner text-left">'; echo '<div class="card"><h1>'. $projectname .'</h1> <p>Campaign id: '. $this->CID .'<p/>'; echo '</div>'; /* Close card */ echo '</div>'; /* Close col-inner text-left */ echo '</div>'; /* Close col medium-4 small-12 large-4 */ }// closing foreach }// closing -if- // Save output and stop output buffering $output = ob_get_clean(); // Return $post to its original state wp_reset_postdata(); // Return buffered output to be output in shortcode location return $output; } // END OF PROJECT_LIST FUNCTION() // START OF CAMPAIGN_PROJECT_FUNCTION() public function campaign_project($PID) { // Start output buffering so get_template_part doesn't output to the page ob_start(); $api_key = get_option( 'my_personal_api_key' ); if ( empty( $api_key ) ) { return; } $request = wp_remote_get('https://api.domain.com/publications.svc?api_key='.$api_key.'&project='.$this->PID); // URL TO GET THE CAMPAIGN LIST // OUTPUT JSON if( is_wp_error( $request ) ) { return false; // Bail early } $body = wp_remote_retrieve_body( $request ); $data = json_decode( $body ); if( ! empty( $data ) ) { /* Show specific results in html/css */ foreach( $data as $campaign ) { $campaignid = ($campaign->id); $this->CID = $campaignid; echo $this->CID; }// closing foreach }// closing -if- // Save output and stop output buffering $output = ob_get_clean(); // Return $post to its original state wp_reset_postdata(); // Return buffered output to be output in shortcode location return $output; } // END OF CAMPAIGN_PROJECT FUNCTION() /** * Registers all shortcodes at once * @return [type] [description] */ public function register_shortcodes() { add_shortcode( 'project_campaign_list', array( $this, 'project_list' ) ); } // register_shortcodes() }//end of class
- The topic ‘make multiple API calls and use $object in another function’ is closed to new replies.