Set Timeout SOAP client (Zend Framework) - zend-framework

I'm requesting a webservice using SOAP for which I need to set a request timeout.
new Zend_Soap_Client(http://www.aaa.com/ws/Estimate.asmx?wsdl",
array('encoding' => 'UTF-8');
I have also tried passing 'connection_timeout'=>100 but it seems like "unknow SOAP client option". Please suggest a way I can set the set timeout.
Thanks

I found a solution to set the timeout with Zend_Framework:
If you have your SoapClient-Object like this:
$client = new Zend_Soap_Client(http://www.aaa.com/ws/Estimate.asmx?wsdl", array('encoding' => 'UTF-8');
You can set the timeout for HTTP-Requests. The default timeout in PHP is 30 seconds. With the following code you can e.g. set it to 1 minute.
$context = stream_context_create(
array(
'http' => array(
'timeout' => 1000
)
)
);
$client->setStreamContext($context);
Found on downlifesroad.com

Connection timeout option is not supported, the code is present in Zend_Soap_Client but commented
// Not used now
// case 'connection_timeout':
// $this->_connection_timeout = $value;
// break;

ini_set('default_socket_timeout',$seconds);

Here is a suggested solution using ZendHttpClient and Zend_Http_Client_Adapter_Curl.
$client = new Zend_Http_Client($location);
$adapter = new Zend_Http_Client_Adapter_Curl();
$client->setAdapter($adapter);
$adapter->setCurlOption(CURLOPT_TIMEOUT, $this->_timeout);
$client->setMethod(Zend_Http_Client::POST);
$client->setHeaders('Content-Type', $version == 2 ?
'application/soap+xml' : 'text/xml');
$client->setHeaders('SOAPAction', $action);
The idea is that you send an http request with the SOAP envelope as string at the request.
Full gist code here

I solved this issue by using native PHP SoapClient class...
$client = new SoapClient($url,
array(
'connection_timeout'=>'30'
));
$response = $client->wsMethod(array
('param'=>'value));
You can define the whole duration limit using
ini_set('default_socket_timeout', '30');
Before calling it.
Works like a charm... ;)

Related

base_uri not being based from guzzle client instantiation

I'm using lumen trying to set up simple api requests via guzzle.
The problem is the base_uri parameter doesn't appear to be passed correctly on the initial new Client().
Simplified example:
use GuzzleHttp\Client;
$client = new Client([
'base_uri' => 'https://siteurl.com/api/v2'
]);
Then calling the api via get
$res = $client->get('orders', [
'query' => [
'status' => 'completed'
]
]);
does not work. I've been careful not to use absolute urls like /orders. If I bypass base_uri entirely and just add it on the get method $client->get('https://siteurl.com/api/v2/orders'), it works.
I'm using:
"laravel/lumen-framework": "5.0.*",
"guzzlehttp/guzzle": "^6.0"
*Follow-up:
I added the debug flag so I could compare the headers, and the noticable difference is in the get request line.
Absolute url in the get method (bypassing base_uri):
GET /api/v2/orders?status=completed HTTP/1.1
Using base_uri (version is being stripped):
GET /api/orders?status=completed HTTP/1.1
You need to terminate your base_uri with a forward slash /
E.g.,
use GuzzleHttp\Client;
$client = new Client([
'base_uri' => 'https://siteurl.com/api/v2/'
]);
Edit: Note that base_uri is for Guzzle 6+, whereas previous versions used base_url.

Authorization http header is not working at Zend Soap Client

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.

SugarCRM SOAP test call fails

I am attempting to test a SugarCRM Soap connection using the following code:
<?
define('sugarEntry', TRUE);
require_once('include/nusoap/nusoap.php');
$sugarclient = new nusoapclient('http://www.mycrmurl.com/soap.php?wsdl',true);
echo $sugarclient->call('test', 'test string');
?>
Unfortunately, the test call returns NULL. Thoughts on how to begin troubleshooting?
I'm not familiar with a SugarCRM SOAP method called test, so unless it's a custom method you made yourself, I'd try with some simple valid calls. (Tested with Sugar CE 6.2).
<?php
require_once('include/nusoap/lib/nusoap.php');
$myWsdl = 'http://www.mycrmurl.com/soap.php?wsdl';
$myAuth = array(
'user_name' => 'will',
'password' => MD5('will'),
'version' => '0.1'
);
$soapClient = new nusoap_client($myWsdl,true);
$loginParams = array('user_auth' => $myAuth, 'application_name' => 'MyApp');
$loginResult = $soapClient->call('login', $loginParams);
$sessionId = $loginResult['id'];
echo $sessionId;
?>
If the above still gives you problems, try the following:
Look in the web server log (Is the call getting through)
Enable the SugarCRM logging and set the level to debug
Either enable PHP error output or make PHP log errors to a log file
Use e.g. SoapUI to test SOAP call
See question 5396302 for a more thorough SOAP example
Check the SugarCRM SOAP documentation
Do this:
$result = $sugarclient->call('test', 'test string');
echo print_r ($result);
It will print the array result, if you just want to see the error description do this:
$result = $sugarclient->call('test', 'test string');
echo $result['error']['description'];
The result is a multidimensional array.

Linkedin OAuth and Zend retrieving Acces Token returns 'Error in HTTP request'

Answer + new question
I found out that the code below works just fine on a LIVE server. LinkedIN blocked all requests from localhost.
That established; Does anybody know how to test an application from localhost with LinkedIN OAuth? Because doing this on a live server sucks!
Old Question
I'm trying to connect with Zend_OAuth to LinkedIN. This code used to work, but now it returns an error in http request while I'm trying to retrieve an access token.
Tried checking the LinkedIN api, but the code still seems valid. Tried several scripts but all with the same result.
The config is setup in the preDispatch of my controller
$this->configLinkedin = array(
'version' => '1.0',
'siteUrl' => 'http://'.$_SERVER['HTTP_HOST'].$this->view->baseUrl(false).'/news/index/connectlinkedin',
'callbackUrl' => 'http://'.$_SERVER['HTTP_HOST'].$this->view->baseUrl(false).'/news/index/connectlinkedin',
'requestTokenUrl' => 'https://api.linkedin.com/uas/oauth/requestToken',
'userAuthorisationUrl' => 'https://api.linkedin.com/uas/oauth/authorize',
'accessTokenUrl' => 'https://api.linkedin.com/uas/oauth/accessToken',
'consumerKey' => 'XXX',
'consumerSecret' => 'XXX'
);
And the code in the action to connect to linkedIN is
$this->consumer = new Zend_Oauth_Consumer($this->configLinkedin);
if(!empty($_GET) && isset($_SESSION['LINKEDIN_REQUEST_TOKEN']))
{
$token = $this->consumer->getAccessToken($_GET, unserialize($_SESSION['LINKEDIN_REQUEST_TOKEN']));
// Use HTTP Client with built-in OAuth request handling
$client = $token->getHttpClient($this->configLinkedin);
// Set LinkedIn URI
$client->setUri('https://api.linkedin.com/v1/people/~:(id,first-name,last-name,picture-url)');
// Set Method (GET, POST or PUT)
$client->setMethod(Zend_Http_Client::GET);
// Get Request Response
$response = $client->request();
$this->NewsService->TokenSocialMedia(
$token,
'linkedin',
serialize($response->getBody())
);
$_SESSION['LINKEDIN_REQUEST_TOKEN'] = null;
$this->_helper->flashMessenger(array('message' => $this->view->translate('The CMS is successfully connected to your linkedin account'), 'status' => 'success'));
$this->_helper->redirector('settings#settingSocial', 'index');
}
else
{
$token = $this->consumer->getRequestToken();
$_SESSION['LINKEDIN_REQUEST_TOKEN'] = serialize($token);
$this->consumer->redirect();
}
What am I missing or doing wrong? I use a similair setup for Twitter and that works fine.
UPDATE 20 September 211
I found out that this rule is returning the error:
$token = $this->consumer->getRequestToken();
I'm still clueless why, and reading the linkedin api doesn't help a bit. Will keep you posted.
I got similar problem and after adding openssl extension it was solved
try adding to php.ini this line:
extension=php_openssl.dll
I got the same issue, try to turn off ssl before asking the new consumer :
$httpConfig = array(
'adapter' => 'Zend\Http\Client\Adapter\Socket',
'sslverifypeer' => false
);
$httpClient = new HTTPClient(null, $httpConfig);
OAuth::setHttpClient($httpClient);

Migrating SOAP functionality from PHP to Perl using SOAP::WSDL

I'm struggling to get SOAP working in perl.
In PHP it works perfectly, and without any trouble. And now I'm trying to do the same thing in Perl.
The functional PHP is:
$client = new SoapClient("http://thirdpartyurl.com/api.aspx?WSDL");
$info = $client->GetSomeInfo(array('username' => 'myusername', 'password' => 'mypassword'));
Which works great, but I just can't get it working in perl. I tried SOAP::Lite, but didn't make any progress. And I'm now trying SOAP::WSDL:
use SOAP::WSDL;
my $wsdl = SOAP::WSDL->new(wsdl => 'http://thirdpartyurl.com/api.aspx?WSDL');
my $info = $wsdl->call('GetSomeInfo', 'username', 'myusername', 'password', 'mypassword');
Which just doesn't work.
I looked at the raw requests, and the perl version isn't even sending the user/pass parameters through. What am I doing wrong?
For what it's worth, I achieved authentication, and managed to get parameters sent by using the following:
use SOAP::Lite;
my $service = SOAP::Lite->proxy($service_url)->uri($service_url);
sub SOAP::Transport::HTTP::Client::get_basic_credentials {
return 'myusername' => 'mypassword';
}
my $result = $service->insert(
SOAP::Data->name(
'data' => \SOAP::Data->value(
SOAP::Data->name(
'item' => \SOAP::Data->value(
SOAP::Data->name('key' => 'name'),
SOAP::Data->name('value' => 'new_campaign_x')
)
)
)
)->type('Map')
);
Is there a better way to achieve the same results? I realise overwriting 'get_basic_credentials' is a bit hacky.
I've had pretty good luck with SOAP::Lite in my applications. The username/password combo, is that supposed to be authenticating at the HTTP layer or the SOAP layer? Or just regular soap parameters?
When I want to do SOAP, I reach for XML::Compile.