Reputation: 76
Im' looking to consume a Rest API using Zend Framework 2.
I tried :
How to consume a Rest API using Zend Framework 2
$request = new Request();
$request->getHeaders()->addHeaders(array(
'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'
));
$request->setUri($url);
$request->setMethod('POST');
$request->setPost(new Parameters(array('param1' => 'val1')));
$client = new Client($url, array(
'sslverifypeer' => null,
'sslallowselfsigned' => null,
));
$response = $client->dispatch($request);
$data = json_decode($response->getBody(), true);
Can someone provide an example how to enter username and password :
I tried this :
$request->getHeaders()->addHeaders(array(
'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8',
'user' => 'username1',
'password' => 'password1'
));
Upvotes: 1
Views: 744
Reputation: 76
it works now, but with curl :
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "param1=value1",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
"password: password01",
"user: user01"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Upvotes: 0
Reputation: 33148
From https://framework.zend.com/manual/2.4/en/modules/zend.http.client.advanced.html#http-authentication
$client->setAuth('username1', 'password1');
Upvotes: 2