Thank you guys for all the suggestions and answers. I have fixed it, see my code below:
class advanza_form {
// Class properties defined using PHP Coding Guidelines and taking security measures: http://www.php.net/manual/en/language.oop5.visibility.php
public $site_id;
public $affiliate_id;
public $redirection;
protected $url;
private $response;
private $header; // For debugging
public $status_code; // For debugging
public function __construct() {
$this->site_id = get_option('advanza_form_site_id');
$this->affiliate_id = get_option('advanza_form_affiliate_id');
$this->redirection = get_option('advanza_form_thankyou_page');
$this->url = "https://advanzaservices.com/wp/singleform.php?site_id={$this->site_id}&affiliate_id={$this->affiliate_id}&thankyou={$this->redirection}";
$this->response = wp_remote_get( $this->url );
$this->header = wp_remote_retrieve_headers( $this->response ); // array of http header lines
$this->status_code = wp_remote_retrieve_response_code( $this->response );
}
public function display() {
try {
if($this->status_code == 200) {
$html = wp_remote_retrieve_body($this->response);
} else {
return $this->status_code;
}
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}// end try/catch
return $html;
}
public function debug() {
$last_modified = wp_remote_retrieve_header( $this->response, 'last-modified' );
echo '<h1>Debug information</h1>';
echo '<p>';
echo '<strong>Site ID:</strong> ' . $this->site_id . '<br>';
echo '<strong>Affiliate ID:</strong> ' . $this->affiliate_id . '<br>';
echo '<strong>Redirection:</strong> ' . $this->redirection . '<br>';
echo '<strong>URL:</strong> ' . $this->url . '<br>';
echo '</p>';
echo '<h2>Technical details</h2>';
echo '<p>';
echo '<a href="https://en.wikipedia.org/wiki/List_of_HTTP_status_codes" target="_blank"><strong>HTTP status code:</strong></a> ' . $this->status_code . '<br>';
echo '<strong>Headers:</strong> '; var_export($this->header);
echo '</p>';
// error: Invalid or no affiliate_id supplied
}
}
The shortcode
function advanza_form_shortcode() {
// Display the Advanza form with a shortcode
$form = new advanza_form;
// For debugging
// $form->debug();
return $form->display();
}
add_shortcode( 'advanza-form', 'advanza_form_shortcode' );