curl -F "data={json:'code'}" in PHP?
<p>I'm trying to access a third-party API with PHP.</p>

<p>Their command reference is like so:</p>

<pre><code>curl "https://api.example.com/api/" \
  -H "Authorization: BEARER-TOKEN" \
  -F 'data={"language":"EN","latitude":12.345,"longitude":12.345}'
</code></pre>

<p>I've tried:</p>

<pre><code>    $url = 'https://api.example.com/api/';
    $header = array(
        'Content-type: application/json',
        'Authorization: ' . $bearertoken
    );

    $fields = 'data={"language":"EN","latitude":"'.$latitude.'","longitude":"'.$longitude.'"}';

    // I've tried this too:
    $fields = array('data' =&gt; '{"language":"EN","latitude":"'.$latitude.'","longitude":"'.$longitude.'"}');

    $ch = curl_init();

    $options = array(
        CURLOPT_URL =&gt; $url,
        CURLOPT_POST =&gt; true,
        CURLOPT_POSTFIELDS =&gt; $fields,
        CURLOPT_HTTPHEADER =&gt; $header,
        CURLOPT_RETURNTRANSFER =&gt; true,
        CURLOPT_HEADER =&gt; false,
        CURLOPT_FOLLOWLOCATION =&gt; true,
        CURLOPT_MAXREDIRS =&gt; 10,
        CURLOPT_ENCODING =&gt; "",
        CURLOPT_AUTOREFERER =&gt; true,
        CURLOPT_CONNECTTIMEOUT =&gt; 120,
        CURLOPT_TIMEOUT =&gt; 120
    );
    curl_setopt_array($ch, $options);
    $result = curl_exec($ch);`
</code></pre>

<p>But the server responds with a message telling me it didn't get the parameters I'm sending.  When I send the same <code>-F "data={}"</code> from a terminal I get the correct response.</p>