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.
I am also struggling to understand how to properly send (post) a http-body full of json data.
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".
Well sending a payload (an array json encoded or json encoded multidimensional arry) in the http-body.
Here is what I am trying to use right now.
public function mainRequest($url, $apiKey) {
$payload = array(
'test' => 'data'
);
$jsonPayload = urlencode(json_encode($payload));
$encyptedKey = $this->dataEncyptor->hashData($apiKey);
$header = array(
'Content-Type:' => 'application/json',
'Content-Length:' => strlen($jsonPayload),
'x-public' => $apiKey,
'x-hash' => $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;
}
In my error log I get this:
* 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)
> 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
< HTTP/1.1 404 Not Found
< Date: Fri, 17 Oct 2014 03:05:43 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
<
* Connection #0 to host local.nslim.ca left intact
* Closing connection #0
And outputted to the screen is:
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
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
Do the cURL setopt's need to be in any particular order?
Thanks for any help!