integrating with php

Discussion in 'Developers Forum' started by shalini, Nov 10, 2008.

  1. shalini New Member

    iam trying to use kashflow with php.

    can anyone help me to fix this bug.

    "
    An exception occured whilst trying to authenticate you. String reference not set to an instance of a String. Parameter name: s
    "

    it would be better if you provide example using php.
  2. DuaneJackson Administrator

    Can you post the PHP code you are using?
  3. shalini New Member

    <?php
    // Pull in the NuSOAP code
    require_once('nusoap-0.7.3/lib/nusoap.php');

    // Create the client instance
    $client = new soapclient('https://secure.kashflow.co.uk/api/service.asmx');
    $client->soap_defencoding = 'utf-8';

    $result = $client->call('GetBankAccounts', array("UserName" => "xxxxxx","Password" => "xxxxxxxxx"),'KashFlow','KashFlow/GetBankAccounts',false,'','rpc','literal');
    print_r($result);

    }
  4. Peter Bowen New Member

  5. DuaneJackson Administrator

    Hi Peter,

    Shalini and I have been exchanging a couple of emails. We've found the problem, but not the solution.

    The raw XML our webservice received from the request above was:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="KashFlow">
    <SOAP-ENV:Body>
    <ns1:GetBankAccounts/>
    <param1>value-you-passed-as-password</param1>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    
    a properly formatted request should look as follows:

    Code:
    <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
    <GetBankAccounts xmlns="KashFlow">
    <UserName>value-you-passed-as-username</UserName>
    <Password>value-you-passed-as-password</Password>
    </GetBankAccounts>
    </soap:Body>
    </soap:Envelope>
    PHP isn't my forte, so although I can spot the problem, I don't have the solution.
  6. Peter Bowen New Member

    Got over this hurdle. I hired an expert at Elance and he figured out the problems in a few minutes.

    I paid for the code so I guess I can distribute it here. This is just the guts of the code - give me a shout if it doesn't make sense.

    It works 100% off the development server but on my production server it still gives an error.

    Warning: SoapClient::__construct() [function.SoapClient---construct]: SSL: fatal protocol error in /var/www/vhosts/getting-paid.com/httpdocs/system/testkf-2.php on line 33

    Googling for this suggests that it might be a problem with IIS. The guy who made the code for me suggested running without openSSL but that means recompiling php which is not going to happen on the production server.

    If you get the code to work without a warning I'd appreciate some pointers.

    Cheers

    Pete
    PHP:
    <?php
    set_time_limit
    (3600);
    ini_set("soap.wsdl_cache_enabled""0");
    ini_set("session.gc_maxlifetime","3600");

    class 
    MSSoapClient extends SoapClient {
        private 
    $namespace;
        function 
    __doRequest($request$location$action$version) {
            

            
    $request preg_replace('/<ns1:(\w+)/''<$1 xmlns="'.$this->namespace.'"'$request1);
            
    $request preg_replace('/<ns1:(\w+)/''<$1'$request);
            
    $request str_replace(array('/ns1:''xmlns:ns1="'.$this->namespace.'"'), array('/'''), $request);
            
            
    // parent call
            
    return parent::__doRequest($request$location$action$version);
        }
        function 
    setNamespace($sNamespace)
        {
            
    $this->namespace=$sNamespace;
        }
        
    }

     if (
    $_POST)
     {
         
    $sUser="enterusernamehere";
         
    $sPassword="enterpasswordhere";

        
         
    $client = new MSSoapClient('https://secure.kashflow.co.uk/api/service.asmx?WSDL',array(
                                                                                            
    'trace'=>true,
                                                                                                ));
        
         
    $client->setNamespace("KashFlow");
         if(isset(
    $_POST['bGetCustomerByID']))
         {
             
    $args = array ("UserName"=>$sUser"Password"=>$sPassword"CustomerID"=>$_POST['txtCustomerId']);
             
    $oResponse $client->getCustomerByID($args);       
         }
         else if(isset(
    $_POST["bGetInvoice"]))
         {
             
    $args = array ("UserName"=>$sUser"Password"=>$sPassword"InvoiceNumber"=>$_POST['txtInvoiceId']);
             
    $oResponse $client->getInvoice($args);       
         }    
         else if(isset(
    $_POST["bGetInvoiceByDateRange"]))
         {
             
    $args = array ("UserName"=>$sUser"Password"=>$sPassword"StartDate"=>$_POST['txtStartDate'],"EndDate"=>$_POST['txtEndDate']);
             
    $oResponse $client->GetInvoicesByDateRange($args);       
         }
         
         echo 
    '<pre>';
         
    print_r($oResponse);
         echo 
    '</pre>';
         echo 
    '<br>';

    }
    ?>
  7. Peter Bowen New Member

  8. DuaneJackson Administrator

    Thanks for posting the code. I'm sure it'll be useful to others too.

    We're actually in the process of working on v2 of the APi with much more functionality. When we releases it, with new documentations we'll be trying to include some sample code.

    D,
  9. Peter Bowen New Member

    Sounds good.

    I've just been ignoring the php warning message - the data comes out of kashflow fine in spite of it.

    Cheers

    Pete
  10. websnail New Member

    Just to note thanks for sharing the intel... I'll be taking a long hard look at this stuff soon with a view to integrating with my shopping cart and a few other bits so it all helps.

    Personally I would never ignore errors though because there's always the possibility of your info being vulnerable to an exploit. Just a paranoid thought ;)
  11. pratheesh New Member

    Hi Duane, I tried the getBankAccounts function and get the following Response from the webservice. The Request going out is fine.

    System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.IO.DirectoryNotFoundException: Could not find a part of the path 'c:\KFAPI\KFAPI-log.txt'.
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
    at TraceExtension.WriteInput(SoapMessage message)
    at TraceExtension.ProcessMessage(SoapMessage message)
    at System.Web.Services.Protocols.SoapMessage.RunExtensions(SoapExtension[] extensions, Boolean throwOnException)
    at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()
    at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
    --- End of inner exception stack trace ---


    The php code I use is. All other functions work fine (the one's we've used till now).

    require_once('nusoap.php');
    $client = new soapclient('https://secure.kashflow.co.uk/api/service.asmx?WSDL','wsdl');
    $client->setDefaultRpcParams(true);
    $soapproxy = $client->getProxy();
    $username = 'myuser';
    $password = 'mypassword';
    $arr = array('UserName'=>$username,'Password'=>$password);
    $result=$soapproxy->GetBankAccounts($arr);
    echo '<br>';
    echo '<h2>Request</h2>';
    echo '<pre>' . htmlspecialchars($soapproxy->request, ENT_QUOTES) . '</pre>';
    echo '<h2>Response</h2>';
    echo '<pre>' . htmlspecialchars($soapproxy->response, ENT_QUOTES) . '</pre>';
    echo '<br>';
    print_r($result);
  12. stevieg_83 New Member

    I've never really done anything with APIs before and looking for a good starter.

    What I want to achieve is quite basic, customer to login to my website, view their outstanding invoices.

    I'll be using PHP 5 - any ideas for starters?
  13. pratheesh New Member

    Hi Stevie,
    have you looked at this http://forums.kashflow.co.uk/developers-forum/261-php-api.html#post745
    That should get you started with the api. You could store the customers kashflow username & password in your own database and use those while invoking the various api functions.

    https://secure.kashflow.co.uk/api/service.asmx
  14. stevieg_83 New Member

    I looked but that's using nusoap. Don't think this is available on my server.

    I read that with PHP5 you should use integrated SOAP features?
  15. pratheesh New Member

    if your php5 is compiled with soap then you might have to stick to php-soap. but if it is an rpm install then you should be able to remove php-soap easily.
    i first tried with php-soap, didnt quite get it working. nusoap was far easier to work with. its just a single file. you just need to download nusoap.php and put it in your application directory. no install required.
    i use php 5.2 and nusoap 0.7.1

    if you want to stick to php-soap then you could look in the documentation for equivalent function calls.
  16. stevieg_83 New Member

    you lost me at RPM install :)

    PHP settings are configured by my host.

Share This Page