Problems POSTing an array to a custom web service...???

Posted on 2011-04-20
Last Modified: 2012-05-11
I've got a little custom web service I'm trying to build to make things a little easier on myself within another project.  I've got a simple CURL function setup to POST data to my own web service....

function CallService($EndPointURL, $PostData)
      $curl = curl_init();
                  curl_setopt($curl, CURLOPT_VERBOSE, 1);
                  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
                  curl_setopt($curl, CURLOPT_TIMEOUT, 120);
                  curl_setopt($curl, CURLOPT_URL, $EndPointURL);
                  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                  curl_setopt($curl, CURLOPT_POSTFIELDS, $PostData);      
      $response = curl_exec($curl);
      return $response;

So then here's a sample of what I'm setting up to use it with...

$Payments = array();
$Receiver = array('ReceiverEmail' => '','Amount' => '7.50');
$Receiver2 = array('ReceiverEmail' => '','Amount' => '7.50');

$PostData = array(
                              'SenderID' => '',
                              'SenderEmail' => '',
                              'Payments' => $Payments
$GetPayKeyResponse = CallService($domain.'/angelleye/paypal/web-services/get-pay-key.php',$PostData);

If I dumpt out my $PostData array on the calling script I get exactly what I expect...

    [SenderID] =>
    [SenderEmail] =>
    [Payments] => Array
            [0] => Array
                    [ReceiverEmail] =>
                    [Amount] => 7.50

            [1] => Array
                    [ReceiverEmail] =>
                    [Amount] => 7.50



But when I go ahead and pass it to my web service it's losing the Payments array data somehow.  Right now I've got my service setup to do the following...

$Payments = isset($_POST['Payments']) ? $_POST['Payments'] : array();

echo '<pre />';



So then in my calling script I simply echo out the response I get back from the service, but I wind up with what you see at this URL...

It simply outputs the word Array instead of any actual contents of an array.  

So I'm a little bit lost why that's happening.  I always thought I could POST array values just like any other value..??  My text values are going over just fine, but my array ends up as just "Array" and I can't loop through it or anything.

Any information on what I'm doing wrong here would be greatly appreciated.  Thanks!

Question by:Andrew Angell
    LVL 8

    Accepted Solution

    You could serialize() the array before posting it with cUrl.
    Then you can unserialize it when you want to use it:

    $PostData = array(
                                  'SenderID' => '',
                                  'SenderEmail' => '',
                                  'Payments' => serialize($Payments)


    $Payments = isset($_POST['Payments']) ? $_POST['Payments'] : array();

    echo '<pre />';


    LVL 11

    Author Comment

    by:Andrew Angell
    Doh!  Forgot all about serialize().  Works perfectly.  Thanks!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now