Nearsoft\SeleniumClient\Http\HttpClient::execute PHP Method

execute() public method

public execute ( Command $command, $trace = false ) : string
$command Nearsoft\SeleniumClient\Commands\Command
return string The response body
    public function execute(Commands\Command $command, $trace = false)
    {
        if ($command->getUrl() == "" || $command->getHttpMethod() == "") {
            throw new \Exception("Must specify URL and HTTP METHOD");
        }
        $curl = curl_init($command->getUrl());
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=UTF-8', 'Accept: application/json'));
        if ($command->getHttpMethod() == HttpClient::POST) {
            curl_setopt($curl, CURLOPT_POST, true);
            if ($command->getParams() && is_array($command->getParams())) {
                curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($command->getParams()));
            } else {
                curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
            }
        } else {
            if ($command->getHttpMethod() == HttpClient::DELETE) {
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
            } else {
                if ($command->getHttpMethod() == HttpClient::GET) {
                    /*NO ACTION NECESSARY*/
                }
            }
        }
        $rawResponse = trim(curl_exec($curl));
        $responseBody = json_decode($rawResponse, true);
        $responseHeaders = curl_getinfo($curl);
        if ($this->_traceAll || $trace) {
            echo "\n***********************************************************************\n";
            echo "URL: " . $command->getUrl() . "\n";
            echo "METHOD: " . $command->getHttpMethod() . "\n";
            echo "PARAMETERS: ";
            if (is_array($command->getParams())) {
                echo print_r($command->getParams());
            } else {
                echo "NONE";
            }
            echo "\n";
            echo "RESULTS:" . print_r($responseBody);
            echo "\n";
            echo "CURL INFO: ";
            echo print_r($responseHeaders);
            echo "\n***********************************************************************\n";
        }
        curl_close($curl);
        $response = array("headers" => $responseHeaders, "body" => $responseBody);
        return $response;
    }

Usage Example

 public function execute(Commands\Command $command, $trace = false)
 {
     $response = parent::execute($command, $trace);
     $this->validateSeleniumResponseCode($response, $command->getPolling());
     $this->validateHttpCode($response, $command->getPolling());
     return $response;
 }