title: How do I create custom headers and send json data using php curl?
body: <p>Using php curl I am trying to connect to an api I have built. I am having trouble creating a custom header with curl well trying to dictate some header information such as Content-Type, Content-Length.</p>
  
  <p>I am also struggling to understand how to properly send (post) a http-body full of json data.</p>
  
  <p>Essentially what I am trying to do is have the header have the Content-Type, Content-Length, some custom fields like $api => "45234523452345", $hashedKey => "32413211234123".</p>
  
  <p>Well sending a payload (an array json encoded or json encoded multidimensional arry) in the http-body.</p>
  
  <p>Here is what I am trying to use right now.</p>
  
  <pre><code>public function mainRequest($url, $apiKey) {
  
        $payload = array(
          'test' =&gt; 'data'
        );
  
        $jsonPayload = urlencode(json_encode($payload));
  
        $encyptedKey = $this-&gt;dataEncyptor-&gt;hashData($apiKey);
  
        $header = array(
          'Content-Type:'   =&gt; 'application/json',
          'Content-Length:' =&gt; strlen($jsonPayload),
          'x-public'        =&gt; $apiKey,
          'x-hash'          =&gt; $encyptedKey
        );
  
        $fp = fopen(dirname(__FILE__).'/errorlog.txt', 'w');
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_VERBOSE, true);
        curl_setopt($ch, CURLOPT_STDERR, $fp);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonPayload);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         // curl_setopt($ch, CURLOPT_POST, 1);
  
        $result = curl_exec($ch);
        curl_close($ch);
  
        return $result;
      }
  </code></pre>
  
  <p>In my error log I get this:</p>
  
  <pre><code>* About to connect() to local.nslim.ca port 80 (#0)
  *   Trying 127.0.0.1...
  * connected
  * Connected to local.nslim.ca (127.0.0.1) port 80 (#0)
  &gt; POST /datacheck HTTP/1.1
  Host: local.nslim.ca
  Accept: */*
  Content-Length: 29
  Content-Type: application/x-www-form-urlencoded
  
  * upload completely sent off: 29 out of 29 bytes
  &lt; HTTP/1.1 404 Not Found
  &lt; Date: Fri, 17 Oct 2014 03:05:43 GMT
  &lt; Server: Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/0.9.8za DAV/2 PHP/5.5.3
  &lt; X-Powered-By: PHP/5.5.3
  &lt; Content-Length: 514
  &lt; Content-Type: text/html
  &lt; 
  * Connection #0 to host local.nslim.ca left intact
  * Closing connection #0
  </code></pre>
  
  <p>And outputted to the screen is:</p>
  
  <p>HTTP/1.1 404 Not Found Date: Fri, 17 Oct 2014 03:06:07 GMT Server: Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/0.9.8za DAV/2 PHP/5.5.3 X-Powered-By: PHP/5.5.3 Content-Length: 514 Content-Type: text/html
  404 Page Not Found</p>
  
  <p>The page you are looking for could not be found. Check the address bar to ensure your URL is spelled correctly. If all else fails, you can visit our home page at the link below.
  Visit the Home Page</p>
  
  <p>Do the cURL setopt's need to be in any particular order?</p>
  
  <p>Thanks for any help!</p>