Authorization http header is not working at Zend Soap Client - zend-framework

I Used the below code to retrive the categories from the third party site using API, but unfortunately stream context is not able to requested at their API and resulting in the Internal Error.
FYI : It is used under zend framework.
$header = "Authorization: Bearer ".$accestoken."\r\n"."Content-Type:text/xml";//.'Content-Type:application/xml';
$wsdl = 'wsdl url';
$context = stream_context_create(array('http' => array('header' => $header,'method'=>'GET')));
$options = array('stream_context'=>$context,'encoding'=>'ISO-8859-1','exceptions'=>FALSE);
$params = array ('accessToken' => $accestoken);
$response = $client->getAdCategories($params);
print_r($response);
So please find the above code and provide some solution for this issue.

$httpHeaders = array(
'http'=>array(
'protocol_version' => 1.1,
'header' => "Authorization:Bearer ".$accestoken."\r\n" ,
"Connection: close"
));
$context = stream_context_create($httpHeaders);
$soapparams = array(
'stream_context' => $context,
);
$client = new SoapClient($wsdl, $soapparams);
$response = $client->getAdCategories($params);
print_r($response);
Please refer https://bugs.php.net/bug.php?id=49853

OK, I see in the title at least this is a SOAP service you are trying to work with. You should then be using something like the Zend_Soap_Client.
Looks like you have a WSDL... so,
$client = new Zend_Soap_Client("yourwsdl.wsdl");
and then make a request like
$retval = $client->method1(10);
Looking at your code I am not 100% sure what authentication approach is in use. If basic HTTP auth you can just pass username and password as options in the client's constructor.
Setting a header might look something like this:
$auth = new stdClass();
$auth->user = 'joe';
$auth->token = '12345';
$authHeader = new SoapHeader('authNamespace', 'authenticate', $auth);
$client->__setSoapHeaders(array($authHeader));
If you need more help post your WSDL.

Related

JWT: Why am I always getting token_not_provided?

I am sending a PUT request to an API endpoint I have created. Using jwt, I am able to successfully register and get a token back.
Using Postman, my request(s) work perfectly.
I am using Guzzle within my application to send the PUT request. This is what it looks like:
$client = new \Guzzle\Http\Client('http://foo.mysite.dev/api/');
$uri = 'user/123';
$post_data = array(
'token' => eyJ0eXAiOiJKV1QiLCJhbGc..., // whole token
'name' => 'Name',
'email' => name#email.com,
'suspended' => 1,
);
$data = json_encode($post_data);
$request = $client->put($uri, array(
'content-type' => 'application/json'
));
$request->setBody($data);
$response = $request->send();
$json = $response->json();
} catch (\Exception $e) {
error_log('Error: Could not update user:');
error_log($e->getResponse()->getBody());
}
When I log the $data variable to see what it looks like, this is what is returned.
error_log(print_r($data, true));
{"token":"eyJ0eXAiOiJKV1QiL...","name":"Name","email":"name#email.com","suspended":1}
Error: Could not suspend user:
{"error":"token_not_provided"}
It seems like all data is getting populated correctly, I am not sure why the system is not finding the token. Running the "same" query through Postman (as a PUT) along with the same params works great.
Any suggestions are greatly appreciated!
The token should be set in the authorization header, not as a post data parameter
$request->addHeader('Authorization', 'Basic eyJ0eXAiOiJKV1QiL...');

Symfony2 - How to get custom header while testing REST API

I'm testing REST-API. In my DefaultTestController
$client = static::createClient();
$crawler = $client->request(
'GET',
"someurl",
[],
[],
[
'HTTP_X_AUTH_TOKEN' => $clientAuthToken,
]
);
In my REST-controller I'm waiting to get x-auth-token header
$request = Request::createFromGlobals();
$authToken = $request->headers->get('x-auth-token');
but I don't. Whats I do wrong?
In your REST-CONTROLLER you can access to the current request passing it to the methods as follow example:
public function someAction(Request $request)
{
$authToken = $request->headers->get('x-auth-token');
}
Instead of creating a new empty one.
Hope this help

Guzzle 6: Get URL that was "resolved" from base_uri

In Guzzle 3 you can get the resolved URL (without actually opening it) like this:
$client = new Client([
'base_uri' => 'http://foo.com',
]);
$request = $client->get('bar.html');
echo $request->getUrl();
In Guzzle 6 this is not working anymore. Is there another way to get "http://foo.com/bar.html"?
You can use the history middleware, works as advertised:
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;
$container = [];
$stack = HandlerStack::create();
$stack->push(Middleware::history($container));
$client = new Client([
'base_uri' => 'http://foo.com',
'handler' => $stack,
]);
$response = $client->request('GET', 'bar.html');
/* #var RequestInterface $request */
$request = $container[0]['request'];
echo $request->getUri();
For reference, see http://docs.guzzlephp.org/en/latest/testing.html#history-middleware.
It is a bit late, but for the reference.
You can do it with \GuzzleHttp\Psr7\UriResolver::resolve($baseUri, $relUri);
It converts the relative URI into a new URI that is resolved against the base URI.
$baseUri and $relUri are instances of \Psr\Http\Message\UriInterfaceUriInterface.

PHPUnit - REST API testing

I have REST API written in php, i want to test it with phpunit.
I wrote test like this, it works but response body was empty. I tested it with fiddler, it send response body.
Sorry for my english.
class ProgrammerControllerTest extends PHPUnit_Framework_TestCase
{
public function testPOST()
{
// create our http client (Guzzle)
$client = new Guzzle\Http\Client();
$response = $client->post("http://api.loc/v2/", array(
'headers' => "User-Agent: Fiddler\r\n" .
"Host: api.loc\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: 34\r\n",
'body' => array('kle' =>'sino','lat' => '41', 'long' => '69'),
));
var_dump($response);
}
}
Can you try with
var_dump($response->getBody()->getContents());
this is my snippet ( it's in Get but it's the same )
$client = new GuzzleHttp\Client();
$response = $client->get('http://192.168.99.100/v1/hello');
var_dump($response->getBody()->getContents());
result:
string(13) "{"bar":"foo"}"
You can use built in method call of Laravel to test your controllers.
$response = $this->call('POST', '/api/v1.0/pages', $parameters);
$data = $response->getData();
Your ProgrammerControllerTest should extend from TestCase
I know Amazon uses Guzzle to test AWS SDK, you can read more info at http://guzzle3.readthedocs.org/testing/unit-testing.html
And also, don't hesitate to dig what others are using (such as Amazon, Facebook etc...), thats why open-source is so great!

Getting Response Body using Zend_http_Client

I am succesfully calling a REST API with the following code
$client = new Zend_Http_Client();
$client->setMethod(Zend_Http_Client::POST);
$client->setUri('http://www.example.com/api/type/');
$client->setParameterPost(array(
'useremail' => '******#*****.***',
'apikey' => 'secretkey',
'description' => 'TEST WEB API',
'amount' => '5000.00'
));
However I would like to get both the header value-(201) and Response Body that are returned after the execution.
How do I proceed with that?
I am assuming that you're actually executing the request via:
$response = $client->request();
At that point all you need is in the $response object,
//Dump headers
print_r($response->headers);
//Dump body
echo $response->getBody();
Refer to the Zend_Http_Response docs at:
http://framework.zend.com/apidoc/1.10/
for more methods that are available.
this should work...
$client->setUri ( $image_source_urls );
$response = $client->request ( 'GET' );
$folder_content = $response->getBody ();