title: Sending data to a webservice using post
body: <p>I've been provided with an example of how to connect to a certain webserver.</p>
  
  <p>It's a simple form with two inputs:</p>
  
  <p><img src="https://i.stack.imgur.com/OHVIq.png" alt="Webservice Form"></p>
  
  <p>It returns true after submitting the form with both the token and the json.</p>
  
  <p>This is the code:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
      &lt;head&gt;
          &lt;meta charset="utf-8" /&gt;
          &lt;title&gt;Webservice JSON&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;form action="http://foowebservice.com/post.wd" method="post"&gt;
              &lt;p&gt;
                  &lt;label for="from"&gt;token: &lt;/label&gt;
                  &lt;input type="text" id="token" name="token"&gt;&lt;br&gt;
                  &lt;label for="json"&gt;json: &lt;/label&gt;
                  &lt;input type="text" id="json" name="json"&gt;&lt;br&gt;
                  &lt;input type="submit" value="send"&gt;
                  &lt;input type="reset"&gt;
              &lt;/p&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>In order to make it dynamic, I'm trying to replicate it using PHP.</p>
  
  <pre><code>$url = "http://foowebservice.com/post.wd";
  
  $data = array(
    'token' =&gt; 'fooToken',
    'json' =&gt; '{"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);
  </code></pre>
  
  <p>But I must be doing something wrong, because $response it's given a false value.</p>
  
  <p>I don't mind doing this in any other way instead of using Curl.</p>
  
  <p>Any suggestions?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As suggested in the first answer, I've tried to set the $data array in this way:</p>
  
  <pre><code>$data = array(
    'token' =&gt; 'fooToken',
    'json' =&gt; array('foo'=&gt;'test'),
  );
  </code></pre>
  
  <p>However the response was also false.</p>
  
  <p>I've tried the <a href="https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en" rel="nofollow noreferrer">Postman REST - Client plugin</a> for Chrome, and using Development Tools / Network, the url in the headers is:</p>
  
  <pre><code>Request URL:http://foowebservice.com/post.wd?token=fooToken&amp;json={%22foo%22:%22test%22}
  </code></pre>
  
  <p>Which I assume is the same url that should be sent using CURL.</p>