💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] > [github](https://github.com/php-curl-class/php-curl-class) ## 安装 `composer require php-curl-class/php-curl-class` ### demo ``` require __DIR__ . '/vendor/autoload.php'; use \Curl\Curl; $curl = new Curl(); $curl->get('https://www.example.com/'); if ($curl->error) { echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n"; } else { echo 'Response:' . "\n"; var_dump($curl->response); } ``` ## 接口 ### get / post / put / patch / put / delete ``` $curl = new Curl(); $curl->get('https://www.example.com/search', array( 'q' => 'keyword', )); ``` ### 发送文件 ``` $curl = new Curl(); $curl->post ('https://api.example.com/profile/', array( 'image' => '@path/to/file.jpg', )); //or $curl = new Curl(); $curl->post ('https://api.example.com/profile/', array( 'image' => new CURLFile('path/to/file.jpg'), )); ``` ### 设置 header 信息 ``` $curl = new Curl(); $curl->setBasicAuthentication('username', 'password'); $curl->setUserAgent('MyUserAgent/0.0.1 (+https://www.example.com/bot.html)'); $curl->setReferrer('https://www.example.com/url?url=https%3A%2F%2Fwww.example.com%2F'); $curl->setHeader('X-Requested-With', 'XMLHttpRequest'); $curl->setCookie('key', 'value'); $curl->get('https://www.example.com/'); if ($curl->error) { echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n"; } else { echo 'Response:' . "\n"; var_dump($curl->response); } var_dump($curl->requestHeaders); var_dump($curl->responseHeaders); ``` ### 下载文件 ``` $curl = new Curl(); $curl->setOpt(CURLOPT_ENCODING , ''); $curl->download("http://csstools.chinaz.com/tools/images/public/ticon.png", "b.png"); echo $curl->responseHeaders['Content-Type'] . "\n"; // image/png $curl->close(); ``` ### 并发发送 MultiCurl ``` use \Curl\MultiCurl; // Requests in parallel with callback functions. $multi_curl = new MultiCurl(); /*成功时候触发*/ $multi_curl->success(function($instance) { echo 'call to "' . $instance->url . '" was successful.' . "\n"; echo 'response:' . "\n"; var_dump($instance->response); }); /*失败时触发*/ $multi_curl->error(function($instance) { echo 'call to "' . $instance->url . '" was unsuccessful.' . "\n"; echo 'error code: ' . $instance->errorCode . "\n"; echo 'error message: ' . $instance->errorMessage . "\n"; }); /*全部执行完触发*/ $multi_curl->complete(function($instance) { echo 'call completed' . "\n"; }); $multi_curl->addGet('https://www.baidu.com/search', array( 'q' => 'hello world', )); $multi_curl->addGet('https://duckduckgo.com/', array( 'q' => 'hello world', )); $multi_curl->addGet('https://www.bing.com/search', array( 'q' => 'hello world', )); $multi_curl->start(); // Blocks until all items in the queue have been processed. ```