在PHP中的cURL中未设置内容类型 [英] Content Type not being set in cURL in PHP

查看:237
本文介绍了在PHP中的cURL中未设置内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用cURL发送一个请求到一个API(Xero,具体),但我显然发送空请求。我检查了cURL信息,看起来我没有Content-Type集合,即使我已经在代码中设置它。

I'm trying to send a request to an API (Xero, to be specific) using cURL but I was apparently sending empty requests. I checked the cURL info and it looks like I don't have a Content-Type set, even though I've been setting it in the code.

这里是我的代码:

    $content = $this->getContent();
    $headers = [
        "Content-Type: application/x-www-form-urlencoded",
        "Content-Length: " . strlen($content),
        "Connection: close"
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this->getUrl());
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HEADER, true);

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $content);

    de($headers, curl_getinfo($ch));

exit函数,并且输出:

de() is a a "dump and exit" function, and it outputs this:

 array(3) {
  [0] =>
      string(47) "Content-Type: application/x-www-form-urlencoded"
  [1] =>
      string(20) "Content-Length: 1003"
  [2] =>
      string(17) "Connection: close"
}

array(26) {
  'url' =>
  string(41) "https://api.xero.com/api.xro/2.0/Invoices"
  'content_type' =>
  NULL
  'http_code' =>
  int(0)
  'header_size' =>
  int(0)
  'request_size' =>
  int(0)
  'filetime' =>
  int(0)
  'ssl_verify_result' =>
  int(0)
  'redirect_count' =>
  int(0)
  'total_time' =>
  double(0)
  'namelookup_time' =>
  double(0)
  'connect_time' =>
  double(0)
  'pretransfer_time' =>
  double(0)
  'size_upload' =>
  double(0)
  'size_download' =>
  double(0)
  'speed_download' =>
  double(0)
  'speed_upload' =>
  double(0)
  'download_content_length' =>
  double(-1)
  'upload_content_length' =>
  double(-1)
  'starttransfer_time' =>
  double(0)
  'redirect_time' =>
  double(0)
  'certinfo' =>
  array(0) {
    }
  'primary_ip' =>
  string(0) ""
  'primary_port' =>
  int(0)
  'local_ip' =>
  string(0) ""
  'local_port' =>
  int(0)
  'redirect_url' =>
  string(0) ""
}

我正确设置标题(常量拼写正确,我没有设置它们多次)。

As far as I can see I am setting the headers correctly (the constant is spelled correctly and I haven't set them multiple times). What am I doing wrong?

推荐答案

首先使用 curl_error a href =http://in2.php.net/curl_error =nofollow noreferrer>点击我了解详情!!

First check with curl_error function click me for detail!!

url是 https 。因此会执行SSL验证程序。

since your url is https. so ssl verifying process is followed.

非正式修正:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

正式修复:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/CAcerts/your_certificate.crt");

证书在哪里?


  1. 在Firefox浏览器中浏览网址


  1. 点击地址左边的锁定bar

  1. Click lock pad at left of address bar

然后点击更多信息

Then click more information

现在点击查看证书按钮

Now click the view certificate button

在打开的弹出窗口中,

In opened pop up you will see two tab in that click details tab (shortcut ALT + D).

然后点击导出按钮并保存证书

Then click export button and save the certificate


  1. 这里是您必须提供的路径

  1. That is the path you have to give here

curl_setopt($ ch,CURLOPT_CAINFO,getcwd()。/CAcerts/your_certificate.crt)

curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/CAcerts/your_certificate.crt")

这篇关于在PHP中的cURL中未设置内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆