SOAP Client Request --> Bad Request - soap

I've been asked to look into syncing data using a SOAP service. I don't really know SOAP very well at all and I get a Bad Request Error.
The function I'm trying to call is a test echo function:
public string EchoAuthenticated(string text)
Each time I call it I get an error.
I have commented out the username / password setting as I don't know the username and password right now and my contact person is on leave :( Right now though I'd be perfectly happy just to get an authentication failed message rather than an error...
If anyone could point me in the right direction here please...
Thanks,
John
<?php
$apiUrl = 'https://exdev.api.propctrl.co.za/v3/Integration.svc?wsdl';
$options = array( 'trace' => 1, 'exceptions' => 1, 'soap_version' => SOAP_1_2);
try
{
$client = new SoapClient($apiUrl, $options);
//$data = array(
// 'Username' => "test",
// 'Password' => "test"
//);
//$header = new SoapHeader('https://exdev.api.propctrl.co.za/v3/', 'CredentialsHeader', $data, false);
//$client->__setSoapHeaders($header);
var_dump($client->__getFunctions());
print $client->EchoAuthenticated("Test String");
var_dump($client->__getLastRequest());
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>

You might try something like:
...
$client = new SoapClient($apiUrl, $options);
var_dump($client->__getFunctions());
$auth = array("Username" => "John", "Password" => "secret",
"IsP24Credentials" => false);
$header = new SoapHeader("https://www.propctrl.com/", "CredentialsHeader",
$auth, FALSE);
$client->__setSoapHeaders($header);
print $client->EchoAuthenticated(array(
"text" => "My text to be echoed."
));
var_dump($client->__getLastRequest());
...
This should result in a request SOAP request like this:
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="https://www.propctrl.com/v3" xmlns:ns2="https://www.propctrl.com/">
<env:Header>
<ns2:CredentialsHeader>
<ns2:IsP24Credentials>false</ns2:IsP24Credentials>
<ns2:Password>secret</ns2:Password>
<ns2:Username>John</ns2:Username>
</ns2:CredentialsHeader>
</env:Header>
<env:Body>
<ns1:EchoAuthenticated>
<ns1:text>My text to be echoed.</ns1:text>
</ns1:EchoAuthenticated>
</env:Body>
</env:Envelope>
As a side note, you might have a look at http://www.soapui.org/. This tool helps greatly with web service development.

Related

How to Retrieve HTTP Status Code with Guzzle?

New to Guzzle/Http.
I have a API rest url login that answer with 401 code if not authorized, or 400 if missing values.
I would get the http status code to check if there is some issues, but cannot have only the code (integer or string).
This is my piece of code, I did use instruction here ( http://docs.guzzlephp.org/en/stable/quickstart.html#exceptions )
namespace controllers;
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\ClientException;
$client = new \GuzzleHttp\Client();
$url = $this->getBaseDomain().'/api/v1/login';
try {
$res = $client->request('POST', $url, [
'form_params' => [
'username' => 'abc',
'password' => '123'
]
]);
} catch (ClientException $e) {
//echo Psr7\str($e->getRequest());
echo Psr7\str($e->getResponse());
}
You can use the getStatusCode function.
$response = $client->request('GET', $url);
$statusCode = $response->getStatusCode();
Note: If your URL redirects to some other URL then you need to set false value for allow_redirects property to be able to detect initial status code for parent URL.
// On client creation
$client = new GuzzleHttp\Client([
'allow_redirects' => false
]);
// Using with request function
$client->request('GET', '/url/with/redirect', ['allow_redirects' => false]);
If you want to check status code in catch block, then you need to use $exception->getCode()
More about responses
More about allow_redirects
you can also use this code :
$client = new \GuzzleHttp\Client(['base_uri' 'http://...', 'http_errors' => false]);
hope help you

How to integrate instamojo payment gateway with codeigniter rest server?

I am trying to integrate Instamojo Payment Gateway within Chris Kacerguis’ REST Server.
Problem:
The below code:
public function instamojotest_post()
{
$api = new Instamojo\Instamojo(‘abcd1234’, ‘efgh5678’, 'https://test.instamojo.com/api/1.1/');
try {
$response = $api->paymentRequestCreate([
'amount' => 100,
'purpose' => 'New Product Purchase',
'buyer_name' => 'Test User',
'email' => 'testuser#gmail.com',
'phone' => '9876543210',
'redirect_url' => 'http://www.example.com/products_api/validate_payment'
]);
header('Location: ' . $response['longurl']);
} catch (Exception $e) {
$this->response([
'success' => false,
'message' => $e->getMessage()
], 500);
}
}
is not redirecting to the Instamojo Payment Site and no error is being displayed.
It is working fine and redirecting successfully with vanilla CodeIgniter.
Questions:
1) Is it, at all, possible to redirect from within a REST Server Post Method?
2) If the above is possible, then what is wrong with my code?
3) Is there any other way to achieve what I am trying to do?
I found many tutorials on the internet but none of them are using REST Server.
I stumbled accross this question while Googling. I was also facing the same issue and here is how I solved it.
Note: This is not exactly a solution but a work-around. Also I admit that this may not be the best solution out there, but it worked for me.
I returned the payment url from the Rest Server, and redirected to the url from within the Rest Client.
Rest Client Code:
class Test extends CI_Controller
{
public function instamojo_make_payment()
{
$url = "http://www.example.com/products_api/instamojotest";
$params = []; //You will obviously be needing this in real life implementation :)
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($curl_handle);
curl_close($curl_handle);
if ($response['success'])
header('Location: ' . $response['payment_url']);
else
$this->load->view('payment_failed_page');
}
}
Rest Server Code:
class Products_api extends REST_Controller
{
public function instamojotest_post()
{
$api = new Instamojo\Instamojo('abcd1234', 'efgh5678', 'https://test.instamojo.com/api/1.1/');
try {
$response = $api->paymentRequestCreate([
//Make sure to pass these data from the Rest Client
'amount' => 100,
'purpose' => 'New Product Purchase',
'buyer_name' => 'Test User',
'email' => 'testuser#gmail.com',
'phone' => '9876543210',
'redirect_url' => 'http://www.example.com/products_api/validate_payment'
]);
$this->response([
'success' => true,
'payment_url' => $response['longurl']
], 200);
} catch (Exception $e) {
$this->response([
'success' => false,
'message' => $e->getMessage()
], 500);
}
}
}
While giving this answer I assumed that the Api is open. If it is not, then make sure to pass your credentials when making the curl call.
Update
Thanks to #AshwiniChaudhary's comment below, which states that:
REST APIs are not meant for redirection. REST API returns JSON, XML
etc and the receiver takes care of whatever is supposed to be done.
the actual reason behind the fact, "why REST Server is not letting us to perform the redirect", becomes pretty clear.

How to call soap api with BASIC authantication for WSDL URL

Blockquote
We are using Soap API but we can not connect to server.I am new in soap api.
We are using Code
username = testclient
password = tes#123
try {
$client=new SoapClient($wsdl,array('trace' => 1,"stream_context" => $context));
$result = $client->__call('getStatus', array());
} catch (Exception $e) {
echo $e->getMessage();
}
After passing header.
$client = new SoapClient(
'https://test/app/uat/test?wsdl',
array(
"exceptions" => 0,
"trace" => 1,
'stream_context' => stream_context_create(array(
'http' => array( 'header' => 'Authorization: Basic dGVzdGNsaWVudDp0ZXN0QDEyMw==' ),
)),
));
$result = $client->__soapCall('getBalance', array());
Also we have client and secret but How we can use for BASIC auth?
Where we can set user, pass and key and secret.
If you need any thing please let me know.
Thankyou

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...');

SoapFault exception: [HTTP] Bad Request In eway

I am going to integrate the eway token payment integration and i am facing this problem.
SoapFault exception: [HTTP] Bad Request
the wsdl file is here
https://www.eway.com.au/gateway/ManagedPaymentService/managedCreditCardPayment.asmx?wsdl
and the xml format is here
https://www.eway.com.au/gateway/ManagedPaymentService/test/managedcreditcardpayment.asmx?op=CreateCustomer
and i get the xml file with $client->__getLastRequest(); script is
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="https://www.eway.com.au/gateway/managedpayment" xmlns:ns2="eWAYHeader">
<env:Header>
<ns2:http://www.eway.com.au/gateway/managedPayment>
<item>
<key>eWAYCustomerID</key><value>87654321</value>
</item>
<item><key>Username</key><value>test#eway.com.au</value>
</item>
<item><key>Password</key><value>test123</value>
</item>
</ns2:http://www.eway.com.au/gateway/managedPayment>
</env:Header><env:Body>
<ns1:CreateCustomer>
<ns1:Title>Mr.</ns1:Title>
<ns1:FirstName>Joe</ns1:FirstName>
<ns1:LastName>Bloggs</ns1:LastName>
<ns1:Address>Bloggs Enterprise</ns1:Address>
<ns1:Suburb>Capital City</ns1:Suburb>
<ns1:State>ACT</ns1:State>
<ns1:Company>Bloggs</ns1:Company>
<ns1:PostCode>2111</ns1:PostCode>
<ns1:Country>au</ns1:Country>
<ns1:Email>test#eway.com.au</ns1:Email>
<ns1:Fax>0298989898</ns1:Fax>
<ns1:Phone>0297979797</ns1:Phone>
<ns1:Mobile>9841381980</ns1:Mobile>
<ns1:CustomerRef>Ref123</ns1:CustomerRef>
<ns1:JobDesc>Web developer</ns1:JobDesc>
<ns1:Comments>Please Ship ASASP</ns1:Comments>
<ns1:URL>http://www.test.com.au</ns1:URL>
<ns1:CCNumber>4444333322221111</ns1:CCNumber>
<ns1:CCNameOnCard>Test Account </ns1:CCNameOnCard>
<ns1:CCExpiryMonth>1</ns1:CCExpiryMonth>
<ns1:CCExpiryYear>13</ns1:CCExpiryYear>
</ns1:CreateCustomer>
</env:Body>
</env:Envelope>
Is there both xml structure effets to soap:
Or is this something like problem of soap header?
i have set header like this
$data = array('eWAYCustomerID'=>'87654321',
'Username' => "test#eway.com.au",
'Password' => "test123"
);
$header = new SoapHeader('eWAYHeader',$url,$data);
$client->__setSoapHeaders($header);
I am getting:
SoapFault exception: [HTTP] Bad Request in D:\wamp\www\eway\newfile.php:196
Stack trace:
#0 [internal function]: SoapClient->__doRequest('__call('CreateCustomer', Array)
#2 D:\wamp\www\eway\newfile.php(196): SoapClient->CreateCustomer(Array)
#3 {main}
This error always while i call this function
$customerinfo =
array(
'Title'=>'Mr.',
'FirstName' => 'Joe',
'LastName'=>'Bloggs',
'Address'=>'Bloggs Enterprise',
'Suburb'=>'Capital City',
'State'=>'ACT',
'Company'=>'Bloggs',
'PostCode'=>'2111',
'Country'=>'au',
'Email'=>'test#eway.com.au',
'Fax'=>'0298989898',
'Phone'=>'0297979797',
'Mobile'=>'9841381980',
'CustomerRef'=>'Ref123',
'JobDesc'=>'Web developer',
'Comments'=>'Please Ship ASASP',
'URL'=>'http://www.test.com.au',
'CCNumber'=>'4444333322221111',
'CCNameOnCard'=>'Test Account ',
'CCExpiryMonth'=>'01',
'CCExpiryYear'=>'13'
);
$client->CreateCustomer($customerinfo);
Any help will be more valuable.
Thanks in advance.
Try to use the following code instead:
<?php
$apiUrl = 'https://www.eway.com.au/gateway/ManagedPaymentService/test/managedcreditcardpayment.asmx?WSDL';
$options = array( 'trace' => 1, 'exceptions' => 1);
try{
$client = new SoapClient($apiUrl, $options);
$data = array(
'eWAYCustomerID' => '87654321',
'Username' => "test#eway.com.au",
'Password' => "test123"
);
$header = new SoapHeader('https://www.eway.com.au/gateway/managedpayment', 'eWAYHeader', $data, false);
$client->__setSoapHeaders($header);
$customerinfo = array(
'Title'=>'Mr.',
'FirstName' => 'Joe',
'LastName'=>'Bloggs',
'Address'=>'Bloggs Enterprise',
'Suburb'=>'Capital City',
'State'=>'ACT',
'Company'=>'Bloggs',
'PostCode'=>'2111',
'Country'=>'au',
'Email'=>'test#eway.com.au',
'Fax'=>'0298989898',
'Phone'=>'0297979797',
'Mobile'=>'9841381980',
'CustomerRef'=>'Ref123',
'JobDesc'=>'Web developer',
'Comments'=>'Please Ship ASASP',
'URL'=>'http://www.test.com.au',
'CCNumber'=>'4444333322221111',
'CCNameOnCard'=>'Test Account ',
'CCExpiryMonth'=>'01',
'CCExpiryYear'=>'13'
);
$result = $client->CreateCustomer($customerinfo);
var_dump($result);
}catch(Exception $e){
echo $e->getMessage();
}
which worked for me.
Notes:
1. Always try to wrap the code in try{} catch{} block
2. Make sure to check php_openssl extension is enabled or not
3. Disable the wsdl cache & enable the exceptions
4. Note the SoapHeader constructor.
Hope this helps you.
Regards