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

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效