PHP examples

Discussion in 'Developers Forum' started by Peter Bowen, Apr 7, 2009.

  1. Peter Bowen New Member

    These are working for me, feel free to use them at your own risk
    PHP:

    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;
        }
        
    }
    To make a new customer - I'm only you can extend the function to add other details to the record.
    PHP:
    function newKashflowCustomer($soapClient,$idAccount,$name,$address,$town,$postcode,$country,$email)
    {
        
    //returns customer id if ok else 0
        
        
    $today date("Y-m-d",mktime());
        
        
    $customerArray = array(
            
    "CustomerID"=>"",
            
    "Code"=>"",
            
    "Name"=>"$name",
            
    "Contact"=>"",
            
    "Telephone"=>"",
            
    "Mobile"=>"",
            
    "Fax"=>"",
            
    "Email"=>"$email",
            
    "Address1"=>"$address",
            
    "Address2"=>"",
            
    "Address3"=>"$town",
            
    "Address4"=>"$country",
            
    "Postcode"=>"$postcode",
            
    "Website"=>"",
            
    "EC"=>"0",
            
    "Notes"=>"",
            
    "Source"=>"",
            
    "Discount"=>"0",
            
    "ShowDiscount"=>"0",
            
    "PaymentTerms"=>"0",
            
    "ExtraText1"=>"1",
            
    "ExtraText2"=>"1",
            
    "CheckBox1"=>"1",
            
    "CheckBox2"=>"1",
            
    "Created"=>"$today",
            
    "Updated"=>"$today");
        
        
    $args = array ("UserName"=>"your username here""Password"=>"your password here","custr"=>$customerArray);
        
    $oResponse $soapClient->InsertCustomer($args);
        
    //handle any errors
        
    if($oResponse->Status != "OK")
        {
            echo 
    $oResponse->StatusDetail;
            
    $return 0;

        }
        else
        {
            
    $id $oResponse->InsertCustomerResult;
            
            
    $return $id;
            
        }
        return 
    $return;
    }
  2. ecofinn New Member

    Hi Peter.

    Firstly thanks, the code above was really useful. The MSSoapClient extension of the SoapClient class got it working for me, after a little trial and error.

    However, I would really like to know why it is working.
    I understand that it sets the namespace, but what exactly is happening when you are reformatting the $request variable with the preg_replace and str_replace?

    Any further explanation from anyone who understands this would be most helpful.

    Many thanks.

    Finn.
  3. crantok New Member

  4. encabcompare New Member

    Insert new Customer

    Hi,
    Above example given by peter doesn't work for me it's give and error for XML like
    Array ( [faultcode] => soap:Client [faultstring] => Server was unable to read request. ---> There is an error in XML document (1, 460). ---> Input string was not in a correct format. [detail] => )

    then I found that we CustomerID is auto increment number generated by kashflow so, we don't have to put in XML request.

    Here I put my code to insert new customer hope, it will help you.

    require_once('nusoap/lib/nusoap.php');
    $client = new soap_client('https://securedwebapp.com/api/service.asmx?WSDL','wsdl');
    $client->setDefaultRpcParams(true);
    $soapproxy = $client->getProxy();
    $username = "username";
    $password = "password";


    $code="TEST09";
    $name="Bhavik Tailor";
    $email="bhavik.j.tailor@gmail.com";
    $address="valsad";
    $town="valsad";
    $country="India";
    $postcode="396001";
    $today = date("Y-m-d",mktime());
    $customerArray = array(

    "Code"=>"$code",
    "Name"=>"$name",
    "Contact"=>"",
    "Telephone"=>"",
    "Mobile"=>"",
    "Fax"=>"",
    "Email"=>"$email",
    "Address1"=>"$address",
    "Address2"=>"",
    "Address3"=>"$town",
    "Address4"=>"$country",
    "Postcode"=>"$postcode",
    "Website"=>"",
    "EC"=>"0",
    "OutsideEC"=>"0",
    "Notes"=>"test user",
    "Source"=>"0",
    "Discount"=>"0",
    "ShowDiscount"=>"0",
    "PaymentTerms"=>"0",
    "ExtraText1"=>"1",
    "ExtraText2"=>"1",
    "CheckBox1"=>"1",
    "CheckBox2"=>"1",
    "Created"=>"$today",
    "Updated"=>"$today",
    "CurrencyID"=>"1");

    $args = array ("UserName"=>"$username", "Password"=>"$password","custr"=>$customerArray);
    $oResponse = $soapproxy->InsertCustomer($args);
    print_r($oResponse);
    //handle any errors
    if($oResponse["Status"] != "OK")
    {
    echo $oResponse["StatusDetail"];
    $return = 0;

    }
    else
    {
    $id = $oResponse["InsertCustomerResult"];
    echo $oResponse["StatusDetail"];
    $return = $id;

    }

    Regards,
    Bhavik Tailor:

Share This Page