license_key = @get_option( 'pqc-license', null )['code']; $this->url = PQC_API_PHANES_URI; $this->headers = array( 'PHANES3DP-ASTROPRINT: 1', 'PHANES3DP-HOST: ' . home_url(), 'PHANES3DP-LICENSE-KEY: ' . $this->license_key, ); } public function request() { return $this->send(); } private function send() { $response = $this->makePostCall(); if ( isset( $response['error'] ) && $response['error'] === true && isset( $response['authorization_url'] ) ) { $msg = $response['error_description'] . ' ' . __( 'Authorize Phanes3dp now', 'pqc' ) . ' '; } elseif (! $response || (isset( $response['error'] ) && $response['error'] === true) ) { if (isset($response['error_description'])) { $msg = $response['error_description']; } elseif (isset($response['response'])) { $msg = $response['response']; } else { $msg = 'Something went wrong, please try again'; } } else { $msg = 'Something went wrong, please try again'; } return $msg; } private function makePostCall() { $curl = curl_init(); $headers = $this->headers; $headers[] = "PHANES3DP-INTENT: $this->intent"; curl_setopt_array( $curl, array( CURLOPT_URL => $this->url, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_POST => true, CURLOPT_MAXREDIRS => 5, CURLOPT_HTTPHEADER => $headers, CURLOPT_POSTFIELDS => array( 'data' => json_encode( [] ) ), ) ); $response = curl_exec( $curl ); if ( empty( $response ) ) { // some kind of an error happened $error = curl_error( $curl ); error_log( $error ); curl_close( $curl ); // close cURL handler return array( 'error' => true, 'response' => $error ); } else { $info = curl_getinfo( $curl ); curl_close( $curl ); // close cURL handler if ( $info['http_code'] != 200 && $info['http_code'] != 201 ) { error_log( $response ); return array( 'error' => true, 'response' => $response ); } } // Convert the result from JSON format to a PHP array $json_response = json_decode( $response, true ); return $json_response; } }