php - 从 PHP 对象获取 cURL 命令

标签 php curl php-curl

是否可以从 PHP 的对象中获取 cURL 命令?我的意思是,做这个:

$ch = curl_init('http://'.$host . $url);
//...
$response = curl_exec($ch);

看起来像这样:

curl -X POST -d http://www.google.com/

这可能吗?有技巧或原生方法吗?

最佳答案

无法从 PHP 库 cURL 中获取命令行等效项(选项)来执行工具 curl

工具和 PHP 库都使用 libcurl 来执行客户端 URL 传输,这是一个 C 库。但仅此而已。对于 cURL 中可用的大多数 curl 选项,您很可能拥有 PHP 等效项,但是,这不是规则。

我不认为您只是想使用 curl 从您的应用程序发出请求,如果您愿意,您会使用 PHP 库。也就是说,没有简单的方法可以得到你想要的。可能您需要将命令构建为字符串。像这样:

function getCurlCommand($url, $method = 'GET', array $params = null /* , ... */ )
{
    $cmd = 'curl';
    $params = [];
    if ($method !== 'GET') {
        $params[] = '-X ' . $method;
    }
    if ($data !== null) {
        $params[] = '-d ' . http_build_query($data); 
    }

    // ...

    return $cmd . ' ' . implode(' ', $params) . ' ' . $url;
}

关于php - 从 PHP 对象获取 cURL 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32640428/

相关文章:

PHPExcel 获取相对于给定列的列名

php - 使用 PHP 和 MySQL 登录到管理员 - 成员(member)网站 - 登录不工作

php - 尝试使用 php 从数据库特定列值中选择

php - 如何使用 PHP Curl 或 Guzzle 客户端使用动态用户代理 header

php - 什么是日期时间的正则表达式模式(2008-09-01 12 :35:45 )?

php - 在 PHPcurl_multi 中,是否有与curl_getinfo() 等效的函数来获取curl_multi_exec() 的HTTP 响应代码?

windows - Curl for Windows - curl 无法打开文件 C :\test. txt

gcc - 对 `__ctype_b_loc' 等的 undefined reference

PHP 警告 : PHP Startup: Unable to load dynamic library php_curl. dll 无法找到

php - 为什么 Mac 计算机上的简单 PHP Curl https POST 请求需要 30 秒以上才能完成?