从PHP的对象获取cURL命令

时间:2022-10-25 21:36:17

Is it possible to get the cURL command from the PHP's object? I mean, make this:

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

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

Look like this:

看起来像这样:

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

Is it possible? Is there a trick or a native method?

可能吗?有诀窍还是原生方法?

1 个解决方案

#1


1  

It's not possible to get the command line equivalent (options) to execute the tool curl from the PHP library cURL.

从PHP库cURL执行工具curl是不可能获得命令行等效(选项)。

Both tool and PHP library use libcurl to perform client-side URL transfers, which is a C library. But that's all. Very likely you have a PHP equivalent for most of curl options available in cURL, however, it's not a rule.

工具和PHP库都使用libcurl来执行客户端URL传输,这是一个C库。但就是这样。很可能你有一个PHP等价于cURL中可用的大多数curl选项,但是,它不是一个规则。

I don't think you simply would like to do requests from your application using curl, if you want so, you'd be using PHP library's. That said, there is no easy way to get what you want. Probably you'll need to build the command as a string. Something like this:

我不认为您只是想使用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;
}

#1


1  

It's not possible to get the command line equivalent (options) to execute the tool curl from the PHP library cURL.

从PHP库cURL执行工具curl是不可能获得命令行等效(选项)。

Both tool and PHP library use libcurl to perform client-side URL transfers, which is a C library. But that's all. Very likely you have a PHP equivalent for most of curl options available in cURL, however, it's not a rule.

工具和PHP库都使用libcurl来执行客户端URL传输,这是一个C库。但就是这样。很可能你有一个PHP等价于cURL中可用的大多数curl选项,但是,它不是一个规则。

I don't think you simply would like to do requests from your application using curl, if you want so, you'd be using PHP library's. That said, there is no easy way to get what you want. Probably you'll need to build the command as a string. Something like this:

我不认为您只是想使用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;
}