1
votes

I'm having trouble correcting my syntax and I would appreciate any assistance; I'm attempting to setup my client containing a base URL, and some necessary header information (including a custom security token)

The next step is to POST to the webservice, :

$baseServiceURL = ['base_uri' => 'http://127.0.0.1:8080/service/v1/ws//something/update']; 
$theHeaders = ['Content-Type' => 'application/json', 'Accept' =>  'application/json', 'token' => 'test-token'];

$updateRequestClient = new Client($baseServiceURL, array(
    "request.options" => array(
       "headers" => $theHeaders
    )
));

//var 1 coming from elsewhere
$varNum2 = $q;
$varNum3 = $w;
$varNum4 = $e;
$varNum5 = $r;
$varNum6 = $t;
$varNum7 = 'me';

// json name/value pairs
$updateBody['name1'] = $varNum1;
$updateBody['name2'] = $varNum2;
$updateBody['name3'] = $varNum3;
$updateBody['name4'] = $varNum4;
$updateBody['name5'] = $varNum5;
$updateBody['name6'] = $varNum6;
$updateBody['name7'] = $varNum7;

//send
$updateRequestResponse = $updateRequestClient->post([ 'body' => json_encode($updateBody) ]);

//response 200??
$responseCode = $updateRequestResponse->getStatusCode();

if ($responseCode == "200") {                                                  

echo ("SUCCESS");

}

I'm presented with the following error on my HTML side:

Warning: parse_url() expects parameter 1 to be string, array given in C:\xampp\vendor\guzzlehttp\psr7\src\Uri.php on line 51

Catchable fatal error: Argument 1 passed to GuzzleHttp\Psr7\Uri::applyParts() must be of the type array, null given, called in C:\xampp\vendor\guzzlehttp\psr7\src\Uri.php on line 55 and defined in C:\xampp\vendor\guzzlehttp\psr7\src\Uri.php on line 410

If I change my URL to string $baseServiceURL = (string)('http://127.0.0.1:8080/service/v1/ws//something/update'); I get:

Catchable fatal error: Argument 1 passed to GuzzleHttp\Client::__construct() must be of the type array, string given, called in C:\xampp\htdocs\SSQueryTool\updateDoctor.php on line 79 and defined in C:\xampp\vendor\guzzlehttp\guzzle\src\Client.php on line 62

2

2 Answers

3
votes
$baseServiceURL = 'http://127.0.0.1:8080/service/v1/ws//something/update'; 
$theHeaders = ['Content-Type' => 'application/json', 'Accept' =>  'application/json', 'token' => 'test-token'];

$updateRequestClient = new Client(array(
    'base_uri' => $baseServiceURL,
    'headers' => $theHeaders
));

Client accepts just 1 parameter in the constructor https://github.com/guzzle/guzzle/blob/master/src/Client.php#L62

0
votes

After working on this the whole day I finally got it working, please find my details below:

//PREP PAYLOAD (varNum1 coming from elsewhere)
$varNum2 = $q;
$varNum3 = $w;
$varNum4 = $e;
$varNum5 = $r;
$varNum6 = $t;
$varNum7 = 'me';

//THE WEBSERVICE UPDATE BASE URL 
$baseServiceURL = (string)('http://127.0.0.1:8080/service/v1/ws//something/update'); 

$updateRequestClient = new Client(['timeout'  => 10000.0,]);

//ASSIGN json name/value pairs to body
$updateBody['name1'] = $varNum1;
$updateBody['name2'] = $varNum2;
$updateBody['name3'] = $varNum3;
$updateBody['name4'] = $varNum4;
$updateBody['name5'] = $varNum5;
$updateBody['name6'] = $varNum6;
$updateBody['name7'] = $varNum7;

//SEND AND SAVE RESULT TO updateRequestResponse //TAKE NOTE OF: JSON_FORCE_OBJECT //
$updateRequestResponse = $updateRequestClient->post($baseServiceURL, ['headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json', 'token' => 'test-token'], 'body' => json_encode($updateBody, JSON_FORCE_OBJECT) ]);

$requestResponseCode = $updateRequestResponse->getStatusCode(); // 200?? 

if ($requestResponseCode == "200") {                                                  

echo ("SUCCESS");

}