/** * Send a job request to the WordLift Server APIs. * @param $request The request (created using the createJobRequest method). * @throws Exception */ public function sendJobRequest($request) { $this->logger->trace("[ request :: " . var_export($request, true) . " ]"); $requestURL = $this->requestURL; $consumerKey = get_option($this->consumerKeyOptionName); $callbackURL = site_url($this->callbackURL); $operations = WordLift_HttpOperations::create($requestURL, WordLift_HttpOperations::CONTENT_TYPE_JSON, WordLift_HttpOperations::CONTENT_TYPE_JSON, array("Application-Id" => $this->applicationId, "Consumer-Key" => $consumerKey, "Callback-Url" => $callbackURL)); $response = $operations->post(null, json_encode($request)); if (array_key_exists("headers", $response) && array_key_exists("proxy-transaction-id", $response["headers"])) { return $response["headers"]["proxy-transaction-id"]; } return false; // $params = array( // $this->requestProtocol => array( // "method" => $this->requestHttpMethod, // "header" => array( // "Content-type: $this->requestContentTypeHeader", // "Accept: $this->requestAcceptHeader", // "Application-Id: $this->applicationId", // "Consumer-Key: $consumerKey", // "Callback-Url: $callbackURL", // ), // "content" => json_encode( $request ) // )); // // create the context and open the connection. // $context = stream_context_create($params); // $fileHandle = @fopen( $requestURL, "rb", false, $context); // if ( !$fileHandle ) { // $this->logger->error( "An error occurred while opening the connection [ requestURL :: $this->requestURL ][ params :: " . var_export( $params, true ) . " ]." ); // return false; // } // // get the response. // $response = @stream_get_contents( $fileHandle ); // if ($response === false) { // $this->logger->error( "An error occurred while reading the response from the server [ requestURL :: $this->requestURL ]." ); // return false; // } // $transactionId = null; // foreach ( $http_response_header as $headerLine ) { // $header = explode( ": ", $headerLine ); // if ( "Proxy-Transaction-Id" === $header[0] ) { // $transactionId = $header[1]; // break; // } // } // // decode the response to a job response. // return $transactionId; }
private function getSiteKey() { $operations = WordLift_HttpOperations::create($this->apiUrl, WordLift_HttpOperations::CONTENT_TYPE_JSON, WordLift_HttpOperations::CONTENT_TYPE_JSON); $response = $operations->get(array("url" => $this->getUrl())); if (is_wp_error($response)) { $errorMessage = $response->get_error_message(); echo "<div class=\"error\"><p>"; echo "<strong>Error</strong>: WordLift could not set or get a" . " valid site key ({$errorMessage} while connecting to " . "{$this->apiUrl})."; echo "</p></div>"; return null; } $respObject = json_decode($response["body"]); if (property_exists($respObject, "key")) { return $respObject->key; } return null; }
public function register($requestBody) { $siteKey = get_option("wordlift_site_key"); $operations = WordLift_HttpOperations::create($this->apiUrl, WordLift_HttpOperations::CONTENT_TYPE_JSON, WordLift_HttpOperations::CONTENT_TYPE_JSON, array("Site-Key" => $siteKey)); $response = $operations->post(null, $requestBody); return json_decode($response["body"]); // $options = array( // "http" => array( // "method" => "POST", // "content" => $requestBody, // "header" => "Content-Type: application/json\r\n" . // "Accept: application/json\r\n" . // "Site-Key: $siteKey" // ) // ); // $context = stream_context_create($options); // $result = file_get_contents($this->apiUrl, false, $context); // $response = json_decode($result); // return $response; }