Sending data to a webservice using post

21,585

Solution 1

You're passing the POST data in JSON format, try to pass it in the form k1=v1&k2=v2. For example, add the following after the $data array definition:

foreach($data as $key=>$value) { $content .= $key.'='.$value.'&'; }

then remove the following lines:

$content = json_encode($data);

and

curl_setopt($curl, CURLOPT_HTTPHEADER,
   array("Content-type: application/json"));

Complete code (tested):

test.php

<?
$url = "http://localhost/testaction.php";

$data = array(
  'token' => 'fooToken',
  'json' => '{"foo":"test"}',
);

foreach($data as $key=>$value) { $content .= $key.'='.$value.'&'; }

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

curl_close($curl);

$response = json_decode($json_response, true);
var_dump($response);
?>

testaction.php

<?
echo json_encode($_POST);
?>

output:

array(2) {
  'token' =>
  string(8) "fooToken"
  'json' =>
  string(14) "{"foo":"test"}"
}

Solution 2

Part of $data is already json encoded. Try making $data pure php. ie $data['json']=array('foo'=>'test');

Share:
21,585
rfc1484
Author by

rfc1484

Updated on July 07, 2022

Comments

  • rfc1484
    rfc1484 almost 2 years

    I've been provided with an example of how to connect to a certain webserver.

    It's a simple form with two inputs:

    Webservice Form

    It returns true after submitting the form with both the token and the json.

    This is the code:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title>Webservice JSON</title>
        </head>
        <body>
            <form action="http://foowebservice.com/post.wd" method="post">
                <p>
                    <label for="from">token: </label>
                    <input type="text" id="token" name="token"><br>
                    <label for="json">json: </label>
                    <input type="text" id="json" name="json"><br>
                    <input type="submit" value="send">
                    <input type="reset">
                </p>
            </form>
        </body>
    </html>
    

    In order to make it dynamic, I'm trying to replicate it using PHP.

    $url = "http://foowebservice.com/post.wd";
    
    $data = array(
      'token' => 'fooToken',
      'json' => '{"foo":"test"}',
    );
    
    $content = json_encode($data);
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER,
      array("Content-type: application/json"));
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
    
    $json_response = curl_exec($curl);
    
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    
    curl_close($curl);
    
    $response = json_decode($json_response, true);
    

    But I must be doing something wrong, because $response it's given a false value.

    I don't mind doing this in any other way instead of using Curl.

    Any suggestions?

    UPDATE

    As suggested in the first answer, I've tried to set the $data array in this way:

    $data = array(
      'token' => 'fooToken',
      'json' => array('foo'=>'test'),
    );
    

    However the response was also false.

    I've tried the Postman REST - Client plugin for Chrome, and using Development Tools / Network, the url in the headers is:

    Request URL:http://foowebservice.com/post.wd?token=fooToken&json={%22foo%22:%22test%22}
    

    Which I assume is the same url that should be sent using CURL.

    • Lorenzo Marcon
      Lorenzo Marcon about 11 years
      you're right in your last update, that's what I wrote in my answer :)
    • Lorenzo Marcon
      Lorenzo Marcon about 11 years
      in this case you should check your webservice.. I tested it on my apache instance and it works. I appended the working test complete code to my answer
  • rfc1484
    rfc1484 about 11 years
    This solution worked, I've made a mistake the first time I tried it. Thanks!