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

使用PHP cURL实现框架HTTP请求时遇类未找到错误求助

致命错误:找不到InitPHP\Curl\Curl类

报错信息:

Fatal error: Uncaught Error: Class "InitPHP\Curl\Curl" not found in C:\xampp\htdocs\Raspberry\requests.php:18
错误栈追踪:#0 C:\xampp\htdocs\Raspberry\test.php(13): InitPHP\Curl\Request::get('https://jsonpla...') #1 C:\xampp\htdocs\Raspberry\test.php(25): testRequest() #2 {main} thrown in C:\xampp\htdocs\Raspberry\requests.php on line 18

涉及文件代码

requests.php 原始代码

<?php

declare(strict_types=1);

namespace InitPHP\Curl;

require_once __DIR__ . '/vendor/autoload.php';


use InitPHP\Curl\Curl;  
use Exception;

class Request
{
    static function get(string $url, array $query = [], array $headers = [], array $options = [])
    {
        // Create a new instance of the Curl class
        $curl = new Curl();

        // Set the URL
        $curl->setUrl($url);

        // Construct the full URL with query parameters
        if (!empty($query)) {
            $url .= '?' . http_build_query($query);
        }

        // Set custom headers if provided
        if (!empty($headers)) {
            $curl->setHeaders($headers);
        }

        // Execute the HTTP request using the Curl class
        if ($curl->handler()) {
            // Get the JSON response
            $response = $curl->getResponse('body');

            // Parse JSON response using json_decode
            $jsonData = json_decode($response, true);

            if ($jsonData === null) {
                throw new Exception('Failed to decode JSON response.');
            }

            return $jsonData;
        } else {
            throw new Exception($curl->getError());
        }
    }
}

test.php 原始代码

<?php 
use InitPHP\Curl\Request;

require_once 'requests.php'; // Include the request.php file with your Request class

require_once 'vendor/autoload.php';


function testRequest()
{
    try {
        $url = 'https://jsonplaceholder.typicode.com/posts/1';
        $response = Request::get($url);
    
        // Print the JSON response
        echo "JSON Response:\n";
        print_r($response);
    } catch (Exception $e) {
        echo "Error: " . $e->getMessage();
    }
    
}

// Call your test functions
testRequest();

问题原因及解决步骤

核心原因

错误源于InitPHP\Curl\Curl类无法被自动加载,主要是自动加载文件引入顺序错误和依赖可能未正确安装导致。

解决步骤

  1. 确认依赖安装
    执行Composer命令安装InitPHP Curl组件:

    composer require initphp/curl
    
  2. 修正requests.php的代码顺序
    将require_once __DIR__ . '/vendor/autoload.php';移到命名空间声明之前,确保自动加载在类引用前生效;同时补充更新Curl实例URL的代码:

    <?php
    
    declare(strict_types=1);
    
    require_once __DIR__ . '/vendor/autoload.php';
    
    namespace InitPHP\Curl;
    
    use InitPHP\Curl\Curl;  
    use Exception;
    
    // 剩余代码保持不变,仅需在拼接query后添加$curl->setUrl($url);
    
  3. 修正test.php的引入顺序
    先加载自动加载文件,再引入自定义的Request类:

    <?php 
    require_once 'vendor/autoload.php';
    use InitPHP\Curl\Request;
    
    require_once 'requests.php';
    
    // 剩余代码保持不变
    

修改后的完整代码

修改后的requests.php

<?php

declare(strict_types=1);

require_once __DIR__ . '/vendor/autoload.php';

namespace InitPHP\Curl;

use InitPHP\Curl\Curl;  
use Exception;

class Request
{
    static function get(string $url, array $query = [], array $headers = [], array $options = [])
    {
        $curl = new Curl();
        $curl->setUrl($url);

        if (!empty($query)) {
            $url .= '?' . http_build_query($query);
            $curl->setUrl($url); // 更新Curl实例的URL
        }

        if (!empty($headers)) {
            $curl->setHeaders($headers);
        }

        if ($curl->handler()) {
            $response = $curl->getResponse('body');
            $jsonData = json_decode($response, true);

            if ($jsonData === null) {
                throw new Exception('JSON响应解析失败');
            }

            return $jsonData;
        } else {
            throw new Exception($curl->getError());
        }
    }
}

修改后的test.php

<?php 
require_once 'vendor/autoload.php';
use InitPHP\Curl\Request;

require_once 'requests.php';

function testRequest()
{
    try {
        $url = 'https://jsonplaceholder.typicode.com/posts/1';
        $response = Request::get($url);
    
        echo "JSON响应:\n";
        print_r($response);
    } catch (Exception $e) {
        echo "错误: " . $e->getMessage();
    }
    
}

testRequest();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 23:57:13