doufangpian5545 2014-03-28 09:54
浏览 69

检测使用cURL发出的请求中的内容类型

I am trying to send cURL request to a remote API server with this code:

$ch = curl_init();
$options = array(CURLOPT_URL => 'http://minecms.info/update/index.php',
                   CURLOPT_POST => true,
                   CURLOPT_RETURNTRANSFER => 1,
                   CURLOPT_POSTFIELDS => $data,
                   CURLOPT_HTTPHEADER => array('Content-type: application/json'),
                   CURLOPT_SSL_VERIFYPEER => false
                  );
curl_setopt_array($ch, $options);
$response = curl_exec($ch);

But I don't want users accessing the updates page on their browsers so I set a content type header on the request. The problem is that I don't know how to detect this content type on the remote server. Basically what I want is to check whether the client request has a content type: application/json set if yes it executes the rest of the code if not it just does exit;.

Thank you to anyone who would help in advance.

  • 写回答

1条回答 默认 最新

  • duanniling0018 2014-03-28 10:35
    关注

    You can try using getallheaders() and check whether the Content-Type is in place.

    Give a look at the man http://www.php.net/manual/it/function.getallheaders.php for insights

    ---- EDIT INSIGHTS ----

    And what about this one? (Which I'm currently using)

    public function getAllHeaders()
    {
        if(function_exists('getallheaders'))
        {
            return getallheaders();
        }
    
        $headers = array();
    
        foreach ($this->parameters as $key => $value)
        {
            if (substr($key, 0, 5) == 'HTTP_')
            {
                $headers[str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($key, 5)))))] = $value;
            }
    
            if ($key == "CONTENT_TYPE")
            { 
                $headers["Content-Type"] = $value; 
            }   
        }
    
        return $headers;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题