Zend framework https request - php

I'm trying to make a https request using zend framework 1.11, with http everithing is working fine, but when i change the request url to https://etc.com, I'm not more able to get the response.
i'm trying like a was reading in the manual like that:
$uri='https://url.com';
$adapter = new Zend_Http_Client_Adapter_Curl();
$client = new Zend_Http_Client();
$client->setUri($uri);
$client->setMethod('POST');
$client->setAdapter($adapter);
$adapter->setConfig(array(
'curloptions' => array(
CURLOPT_SSL_VERIFYPEER => false)
));
Log::notice("URL: " . $uri);
$response = $client->setRawData($xml, 'application/xml')->request('POST');
i activated already in my php.ini the php_curl.dll.
So only with https is not working, maybe somebody can tell me what i'm doing wrong.
Thanks.

Likely invalid certificate. If you can't change the server certificate to valid one, you have to set curl option to ignore invalid certificate:
$addapter->setCurlOption(CURLOPT_SSL_VERIFYPEER, false);

Related

PHP SoapClient with BasicAuth

I have a PHP script trying to connect to a WSDL.
I need to allow self signed AND give basic auth details.
Using SOAP UI, when I connect to the WSDL I am prompted for username / password.
I got this working.
I also found out that each request also requires basic auth (so on the request screen, I have to select Auth, then basic, enter same credentials as I used on the prompt).
How to I do this auth in PHP
As I said, I can connect, not a problem, I seem to kill the service or timeout if I try to make a request
<?php
$context = stream_context_create(array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
));
$data = array(
'columnA' => 'dataA',
'columnB' => 'dataB',
'columnC' => 'dataC');
$url = 'https://111.111.111.111:1234/dir/file';
$login = 'username';
$pwd = 'password';
$client = new soapClient(null, array(
'location' => $url,
'uri' => '',
'login' => $login,
'password' => $pwd,
'stream_context' => $context
));
echo "\n\r---connected---\n\r";
$result = $client ->requestName($data);
print_r($result);
?>
My output is
---connected---
Then it seems to hang.
I have tried wrapping it round a try catch and I had the same result.
Any suggestions??
From the Manual soapclient support the http basic auth.
For HTTP authentication, the login and password options can be used to
supply credentials. For making an HTTP connection through a proxy
server, the options proxy_host, proxy_port, proxy_login and
proxy_password are also available. For HTTPS client certificate
authentication use local_cert and passphrase options. An
authentication may be supplied in the authentication option. The
authentication method may be either SOAP_AUTHENTICATION_BASIC
(default) or SOAP_AUTHENTICATION_DIGEST.
$wsdl = "http://example/services/Service?wsdl";
$option = array(
"trace"=>1,
"login"=>"admin",
"password"=>"admin",
);
$client = new SoapClient($wsdl,$option);
But when I initiate the soapclient, it will throw this error
Exception: Unauthorized
I also have tried to put the auth in the url, like
$wsdl = "http://admin:admin#example/services/Service?wsdl";
But it also doesn't works.
Finally I solved it by add authentication to the option. The manual says the authentication default value is the basic auth, but only when I explicitly set it, it can work.
$option = array(
"trace"=>1,
"login"=>"admin",
"password"=>"admin",
"authentication"=>SOAP_AUTHENTICATION_BASIC
);
Try url encoding your username and password inside the url that you are using:
$url = 'http://'.urlencode('yourLogin').':'.urlencode('yourPassword').'#111.111.111.111:1234/dir/file';
Also I don't see you make use of the wsdl in your code example. You can always download a copy of the wsdl locally and then reference that local copy. You can download the wsdl anyway you want (with php, curl, manually).

Webservice response in yii2 framework

I am new to yii framework
I tried web service response using curl post but I receive error code on response
$curl = new curl\Curl();
$response = $curl->setOption(
CURLOPT_POSTFIELDS,
http_build_query(array(
'email' => 'sfdsdfsdf',
'access_token' => 'fdsdsfsdfsdf',
'auth_type' => 'fdsfsfsdfsd'
)
))
->post('http://example.com/login/');
var_dump($curl->responseCode); - 404
I tried above code.
That URL http://example.com/login/ does not exist, and correctly responds with a HTTP 404 error: Page Not Found. (also see: List of HTTP Status Codes)
It seems your code is functioning. You may want to change the example.com url to your actual endpoint.

HTTP 417 error (Expectation Failed) while posting file to web server via Guzzle

I am using Guzzle in Laravel 4 to send file to the remote server which the server will then process. But while posting the file to the server I'm getting the following exception occurs:
Guzzle \ Http \ Exception \ ClientErrorResponseException
Client error response [status code] 417 [reason phrase] Expectation Failed [url] http://example.com/.....
Following is the code that I am using:
use Guzzle\Service\Client as GuzzleClient;
use Guzzle\Plugin\Cookie\Cookie;
use Guzzle\Plugin\Cookie\CookiePlugin;
use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;
$remote_url = 'http://example.com/';
$client = new GuzzleClient($remote_url);
$client->setSslVerification(FALSE);
$cookieJar = new ArrayCookieJar();
// Create a new cookie plugin
$cookiePlugin = new CookiePlugin($cookieJar);
// Add the cookie plugin to the client
$client->addSubscriber($cookiePlugin);
$post_data = array(
'username' => $input['username'],
'password' => $input['password'],
);
$response = $client->post('login', array(), $post_data)->send();
$response_json = $response->json();
if (isset($response_json['error'])) {
throw new Exception($response_json['error']);
}
$current_time = date("Y-m-d-H-i-s");
$file = 'C:\test\test_file.zip';
$request = $client
->post('receiveFile')
->addPostFields(array('current_time'=>$current_time))
->addPostFile('file', $file)
->send();
The authentication of user seems to work fine and the problem starts only when trying to send the file.
The application throws the error only when I'm trying to send the file to the web server. When I try to send the same file to the same application on my local server, I'm getting the results as I expected without any errors.
I looked for similar problems other people might have faced and found one here on SO Posting a file to a web service with Guzzle , but the solution that worked for the OP of that question didn't work for me. What can I do to solve this problem?
It turned out that when sending the request, a Expect header is added to the request. So what I did was remove the Expect header before sending the request, and everything is working as it should. Following is the code that I changed:
$request = $client
->post('receiveFile')
->addPostFields(array('current_time'=>$current_time))
->addPostFile('file', $file)
->removeHeader('Expect')
->send();
I used the removeHeader method to remove the Expect header. Looks like the removeHeader method must be called just before using the send method, because I had used it before the post method and it hadn't worked before.

PHP non blocking soap request

After a user signs up on my website i need to send a soap request in a method that is not blocking to the user. If the soap server is running slow I don't want the end user to have to wait on it. Is there a way I can send the request and let my main PHP application continue to run without waiting from a response from the soap server? If not, is there a way to set a max timeout on the soap request, and handle functionality if the request is greater than a max timeout?
Edit:
I would ideally like to handle this with a max timeout for the request. I have the following:
//ini_set('default_socket_timeout', 1);
$streamOptions = array(
'http'=>array(
'timeout'=>0.01
)
);
$streamContext = stream_context_create($streamOptions);
$wsdl = 'file://' . dirname(__FILE__) . '/Service.wsdl';
try{
if ( file_get_contents( $wsdl ) ) {
$this->_soapClient = new SoapClient($wsdl,
array(
'soap_version' => SOAP_1_2,
'trace' => true,
'stream_context' => $streamContext
)
);
$auth = array('UserName' => $this->_username, 'Password' => $this->_password);
$header = new SoapHeader(self::WEB_SERVICE_URL, "WSUser", $auth);
$this->_soapClient->__setSoapHeaders(array($header));
}//if
}
catch(Exception $e){
echo "we couldnt connect". $e;
}
$this->_soapClient->GetUser();
I set the timeout to 0.01 to try and force the connection to timeout, but the request still seems to fire off. What am I doing wrong here?
I have had the same issues and have implemented solution !
I have implemented
SoapClient::__doRequest();
To allow multiple soap calls using
curl_multi_exec();
Have a look at this asynchronous-soap
Four solutions:
Use AJAX to do the SOAP -> Simplest SOAP example
Use AJAX to call a second PHP file on your server which does the SOAP (best solution imo)
Put the SOAP request to the end of your PHP file(s) (not the deluxe solution)
Use pcntl_fork() and do everything in a second process (I deprecate that, it might not work with every server configuration)
Depending on the way you implement this, PHP has plenty of timeout configurations,
for example socket_set_timeout(), or stream_set_timeout() (http://php.net/manual/en/function.stream-set-timeout.php)

zend http client current url

I am porting a old project written in plain php to zend framework(and I am new to zend), I am using zend http client(with cURL adapter) in zend project to replace the cULR part of old php project. I got stuck-up as I don't know the zend http client alternate for
$landing_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
which return the url of the landing page after any redirection during the cURL request. I could able to be successfully do the following with zend
$config = array(
'adapter' => 'Zend_Http_Client_Adapter_Curl',
'curloptions' => array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
),
);
$redirecting_page_address ='https://www.domain.tld/someredirectingurl';
$client = new Zend_Http_Client($redirecting_page_address, $config);
$response = $client->request();
and got the required page as output using $response->getBody() now I want to know the url of the landed page where $redirecting_page_address redirected to. Thanks in advance.
As Cyril answered for himself, there is no high-level API in zend framework 2 (2.2.8 2014-09-17) to fetch the effective URL. You can, however, fetch the original curl handle and use native PHP functions on it:
// $client would be a Zend_Http_Client
$handle = $client->getAdapter()->getHandle();
$effectiveUrl = curl_getinfo($handle, CURLINFO_EFFECTIVE_URL);
If redirect is done by headers, you can use
if ($response->isRedirect()){
$newUrl = $response->getHeader('Location');
}
In my research over this I found there no way to do this till the current versions of zend framework (2.0.6), maybe in the future versions this facility can be included.

Categories