# Use SoapClient in cakephp

**URL:** <https://discourse.cakephp.org/t/use-soapclient-in-cakephp/8375>\
**Category:** Need Help\
**Created:** [September 13, 2020, 5:09am UTC](https://discourse.cakephp.org/t/use-soapclient-in-cakephp/8375 "2020-09-13T05:09:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![yvesroma190](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/yvesroma190/32/1926_2.png) [@yvesroma190](https://discourse.cakephp.org/u/yvesroma190)\
**Post date:** [September 13, 2020, 5:09am UTC](https://discourse.cakephp.org/t/use-soapclient-in-cakephp/8375/1 "2020-09-13T05:09:19Z")

</div>

Hello everyone, how to use this php code in cakephp 3 by passing the variables?

```
<?php

 ini_set("soap.wsdl_cache_enabled", 0); 
 $url="https://www.paiementpro.net/webservice/OnlineServicePayment_v2.php?wsdl"; 
 $client = new SoapClient($url,array('cache_wsdl' => WSDL_CACHE_NONE)); 
 $array=array( 'merchantId'=>'PP-F105', 
 'countryCurrencyCode'=>'952', 
 'amount'=>1000, 
 'customerId'=>1, 
 'channel'=>'CARD', 
 'customerEmail'=>'t@t.ci', 
 'customerFirstName'=>'Thierry', 
 'customerLastname'=>'Narcisse', 
 'customerPhoneNumber'=>'22507517917', 
 'referenceNumber'=>'878AABCDEFZ'.time(), 
 'notificationURL'=>'http://test.ci/notification/', 
 'returnURL'=>'http://test.ci/return/', 
 'description'=>'achat en ligne', 
 'returnContext'=>'test=2&ok=1&oui=2',
  );
 try{ 
 $response=$client->initTransact($array); 
if($response->Code==0){

//var_dump($response->Sessionid);die();

header("Location:https://www.paiementpro.net/webservice/onlinepayment/processing_v2.php?sessionid=".$response->Sessionid);

}

  }
   catch(Exception $e)
  { 
  echo $e->getMessage();
   }
?>
```

---

<div class="post-metadata">

**Author:** ![dcoturel](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dcoturel/32/1468_2.png) [@dcoturel](https://discourse.cakephp.org/u/dcoturel)\
**Post date:** [September 13, 2020, 9:12pm UTC](https://discourse.cakephp.org/t/use-soapclient-in-cakephp/8375/2 "2020-09-13T21:12:26Z")

</div>

Hi Yves,

It should work. I have implemented the SoapClient recently in a project. Have you tried to execute this code?

The main difference or thing I would suggest is to add the right import:

use SoapClient;

Saludos,

---

<div class="post-metadata">

**Author:** ![yvesroma190](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/yvesroma190/32/1926_2.png) [@yvesroma190](https://discourse.cakephp.org/u/yvesroma190)\
**Post date:** [September 14, 2020, 2:57am UTC](https://discourse.cakephp.org/t/use-soapclient-in-cakephp/8375/3 "2020-09-14T02:57:28Z")

</div>

When I run this single code in a php file it works fine, but in a controller under cakephp I get no response.

---

<div class="post-metadata">

**Author:** ![yvesroma190](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/yvesroma190/32/1926_2.png) [@yvesroma190](https://discourse.cakephp.org/u/yvesroma190)\
**Post date:** [September 14, 2020, 3:00am UTC](https://discourse.cakephp.org/t/use-soapclient-in-cakephp/8375/4 "2020-09-14T03:00:47Z")

</div>

should I create a controller action template file containing the code?  
Should I enter the code in a controller or single action in a template file? I am new to cakephp and I use version 3.8  
Thank you for helping me.

---

<div class="post-metadata">

**Author:** ![dcoturel](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dcoturel/32/1468_2.png) [@dcoturel](https://discourse.cakephp.org/u/dcoturel)\
**Post date:** [September 14, 2020, 12:49pm UTC](https://discourse.cakephp.org/t/use-soapclient-in-cakephp/8375/5 "2020-09-14T12:49:04Z")

</div>

That would depend on your project. In my case, since I invoke this kind of code from scheduled shells, I make the calls inside my model.

But I should clarify that you should never put this kind of code in a template file. This is not code suitable for a view.

Saludos,

---

<div class="post-metadata">

**Author:** ![yvesroma190](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/yvesroma190/32/1926_2.png) [@yvesroma190](https://discourse.cakephp.org/u/yvesroma190)\
**Post date:** [September 15, 2020, 12:41am UTC](https://discourse.cakephp.org/t/use-soapclient-in-cakephp/8375/6 "2020-09-15T00:41:31Z")

</div>

can you give me some guidelines to adapt the code of the file to my project in cakephp. In fact, this project consists of subscribing to offers and then making payments via the code in the file.  
I beg you to guide me. thank you

---

<div class="post-metadata">

**Author:** ![dcoturel](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dcoturel/32/1468_2.png) [@dcoturel](https://discourse.cakephp.org/u/dcoturel)\
**Post date:** [September 18, 2020, 3:01pm UTC](https://discourse.cakephp.org/t/use-soapclient-in-cakephp/8375/7 "2020-09-18T15:01:18Z")

</div>

As far as I know, you could begin by:

1. Defining where to put the code. Wich model or controller should be responsible for this behavior
2. Then, add the dependency to that class:

use SoapClient;

1. Create a method that does the job
2. Put your code in that method

Saludos,

---

<div class="post-metadata">

**Author:** ![yvesroma190](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/yvesroma190/32/1926_2.png) [@yvesroma190](https://discourse.cakephp.org/u/yvesroma190)\
**Post date:** [September 19, 2020, 3:47am UTC](https://discourse.cakephp.org/t/use-soapclient-in-cakephp/8375/8 "2020-09-19T03:47:03Z")

</div>

Thank you. I followed all of your instructions and it works but I see that there is no redirect to the payout site. And I see that it is at the level of  
`header("Location:https://www.paiementpro.net/webservice/onlinepayment/processing_v2.php?sessionid=".$response->Sessionid);`

the redirection is not done. How can I adapt this redirect code to cakephp?  
Thank you for you precious help

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [September 19, 2020, 4:29am UTC](https://discourse.cakephp.org/t/use-soapclient-in-cakephp/8375/9 "2020-09-19T04:29:09Z")

</div>

You’re asking how to [redirect to a different URL in Cake3](https://book.cakephp.org/3/en/controllers.html#redirecting-to-other-pages)?

---

<div class="post-metadata">

**Author:** ![yvesroma190](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/yvesroma190/32/1926_2.png) [@yvesroma190](https://discourse.cakephp.org/u/yvesroma190)\
**Post date:** [September 19, 2020, 5:48am UTC](https://discourse.cakephp.org/t/use-soapclient-in-cakephp/8375/10 "2020-09-19T05:48:04Z")

</div>

thank you I will come back to you
