You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在CodeIgniter中抓取外部网站内容并解决415媒体类型不支持错误?

Fixing 415 Unsupported Media Type When Fetching Content in CodeIgniter

Hey there, let's break down why you're hitting that 415 Unsupported Media Type error and how to fix it.

What's Causing the 415 Error?

The 415 error pops up because the authentication API endpoint you're calling expects a specific request body format, but your current CURL setup is sending a format it doesn't accept.

Looking at your code: when you pass an array directly to CURLOPT_POSTFIELDS, CURL automatically sets the request's Content-Type to multipart/form-data. Most OAuth2 token endpoints (like the one you're using) require application/x-www-form-urlencoded instead. That's the root issue here.

Step-by-Step Fix

Let's adjust your code to send the correct format and headers. Also, we'll move the CURL logic to your CodeIgniter controller (instead of the view) to follow MVC best practices.

Updated Process.php Controller

<?php defined('BASEPATH') OR exit('No direct script access allowed'); 

class Process extends CI_Controller { 
    public function index() {
        // Handle authentication request in the controller
        $curl = curl_init(); 

        $auth_data = array(
            'client_id' => '30b5f5beeccfba1dbcb31941523d5e3b',
            'client_secret' => 'your_actual_client_secret_here', // Fill this in!
            'grant_type' => 'client_credentials'
        );

        // Convert array to URL-encoded string (required for x-www-form-urlencoded)
        $post_fields = http_build_query($auth_data);

        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields);
        curl_setopt($curl, CURLOPT_URL, 'https://authentication.skimapis.com/access_token');
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        
        // Set the Content-Type header the server expects
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/x-www-form-urlencoded'
        ));

        $result = curl_exec($curl); 

        if(!$result){
            die("Connection Failure");
        }

        // Parse JSON response for easier use in the view
        $token_data = json_decode($result, true);
        
        // Pass data to the view
        $data['token_data'] = $token_data;
        $this->load->view('process', $data); 
    } 
}

Updated process.php View

Now your view can focus on displaying results instead of handling business logic:

<?php if(isset($token_data)): ?>
    <h3>Authentication Result:</h3>
    <pre><?php print_r($token_data); ?></pre>
<?php else: ?>
    <p>Failed to fetch authentication data.</p>
<?php endif; ?>

Key Changes Explained

  • http_build_query(): Converts your associative array into a URL-encoded string, which is the standard format for OAuth2 client credential requests.
  • Explicit Content-Type Header: Tells the server exactly what format we're sending, matching its expectations.
  • Moved Logic to Controller: Separates business logic from presentation, making your code cleaner and easier to maintain.

Additional Tips

  • Don't leave your client_secret blank (you had it empty in your original code) — fill it with your actual secret key.
  • Add checks for json_decode success to handle invalid responses from the server.
  • Consider using CodeIgniter's built-in CURL library for more streamlined, maintainable code (raw CURL works too, but the library simplifies setup).

内容的提问来源于stack exchange,提问作者Sakthi Vel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:06:58