doupingzhi9674 2014-10-17 03:15
浏览 41
已采纳

如何使用php curl创建自定义标头并发送json数据?

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!

  • 写回答

1条回答 默认 最新

  • doy57007 2014-10-17 04:45
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 视频编码 十六进制问题
  • ¥15 Xsheii7我安装这个文件的时候跳出来另一个文件已锁定文件的无一部分进程无法访问。这个该怎么解决
  • ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式