1

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!

floor
  • 1,519
  • 11
  • 24

1 Answers1

1

So the obvious question is, do you have the URL correct? 404 doesn't indicate an error on the page, rather that the page isn't there. Should it be datacheck.php?

Also, you are not sending your data as a standard web form so you will want to change the Content-Type header to 'text/json' and retrieve it in your script using php://input.

Edit: see also RAW POST using cURL in PHP

Community
  • 1
  • 1
miken32
  • 42,008
  • 16
  • 111
  • 154
  • yea my url is right because I have gotten to the point of it sending a 101 continue. and when I visit the url via browser it works. I am trying to set the content type to application/json. – floor Oct 17 '14 at 04:54
  • Might help to post the code on the receiving end as well then. If the script is actually there then you must be specifically telling it to send a 404 somewhere in your code. – miken32 Oct 17 '14 at 04:57
  • Sorry, skipped over where you're setting the mime type. Lose the colons after Content-Type; you can see from your log that the mime type is set to application/x-www-form-urlencoded. – miken32 Oct 17 '14 at 05:01
  • Yea the content type isn't being set properly. That is part of my question. Is there a way to set up a header array with key value pairs well setting the content type and content length? – floor Oct 17 '14 at 13:32
  • I also can't post the api code as it is not directly mine. Thank you for your help thus far. – floor Oct 17 '14 at 13:33
  • Like I said, get rid of the colon after Content-Type. I believe cURL will set the Content-Length for you. I'm still pretty sure you're hitting the wrong URL, or that page is very badly coded to return a 404... – miken32 Oct 17 '14 at 17:01