狂饮;使用基本URL,Headers和POST设置客户端

时间:2016-07-06 07:59:10

标签: php json web-services guzzle

我无法纠正我的语法,我很感激任何帮助;我正在尝试设置包含基本URL的客户端,以及一些必要的头信息(包括自定义安全令牌)

下一步是POST到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");

}

我在HTML方面遇到以下错误:

  

警告:parse_url()要求参数1为字符串,在第51行的C:\ xampp \ vendor \ guzzlehttp \ psr7 \ src \ Uri.php中给出数组

     

捕获致命错误:传递给GuzzleHttp \ Psr7 \ Uri :: applyParts()的参数1必须是数组类型,给定null,在C:\ xampp \ vendor \ guzzlehttp \ psr7 \ src \ Uri.php中调用第55行并在第410行的C:\ xampp \ vendor \ guzzlehttp \ psr7 \ src \ Uri.php中定义

如果我将网址更改为字符串$baseServiceURL = (string)('http://127.0.0.1:8080/service/v1/ws//something/update');,我会:

  

可捕获的致命错误:参数1传递给   GuzzleHttp \ Client :: __ construct()必须是array,string类型   给出,在C:\ xampp \ htdocs \ SSQueryTool \ updateDoctor.php中调用   79并在C:\ xampp \ vendor \ guzzlehttp \ guzzle \ src \ Client.php中定义   第62行

2 个答案:

答案 0 :(得分:3)

$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
));

客户端在构造函数中只接受1个参数 https://github.com/guzzle/guzzle/blob/master/src/Client.php#L62

答案 1 :(得分:0)

在这一天工作之后我终于开始工作了,请在下面找到我的详细信息:

//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");

}