从 Javascript 执行 curl?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2804639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:00:48  来源:igfitidea点击:

Executing curl from Javascript?

javascriptcurl

提问by nate

Is there a way to execute curl from Javascript? Thanks.

有没有办法从 Javascript 执行 curl ?谢谢。

回答by Timothy

Nope. You'll need to look at using Ajax/XMLHttpRequest instead. http://en.wikipedia.org/wiki/Ajax_%28programming%29

不。您需要考虑使用 Ajax/XMLHttpRequest。http://en.wikipedia.org/wiki/Ajax_%28programming%29

回答by Christopher Barber

It depends on what curl you are talking about. You are probably asking about cURL, but if you are asking about the Curl programming language, then the answer is yes. You can invoke Curl methods on an object in an embedded Curl subapplet using the applet_invoke method. There are examples of this in the subapplet examples that are included in the Curl Developer's Guide.

这取决于你在说什么卷曲。您可能问的是 cURL,但如果您问的是 Curl 编程语言,那么答案是肯定的。您可以使用 applet_invoke 方法对嵌入的 Curl 子小程序中的对象调用 Curl 方法。在 Curl 开发人员指南中包含的子小程序示例中有这样的示例。

回答by JCM

It depends of which environment is running javascript, if you mean from the browser, nop, it's not possible.

这取决于运行 javascript 的环境,如果你的意思是从浏览器,不,这是不可能的。

But if it's from Node.js, yes it's possible using native modules. Take a look at: https://github.com/JCMais/node-libcurl

但是如果它来自 Node.js,是的,它可以使用原生模块。看一看:https: //github.com/JCMais/node-libcurl

Code example:

代码示例:

var Curl = require( 'node-libcurl' ).Curl;

var curl = new Curl();

curl.setOpt( 'URL', 'http://www.google.com' );
curl.setOpt( 'FOLLOWLOCATION', true );

curl.on( 'end', function( statusCode, body, headers ) {

    console.info( statusCode );
    console.info( '---' );
    console.info( body.length );
    console.info( '---' );
    console.info( this.getInfo( 'TOTAL_TIME' ) );

    this.close();
});

curl.on( 'error', function ( err, errCode ) {

    //do something

    this.close();
});

curl.perform();

Disclaimer: I'm the author, it's a modified version of another module: https://github.com/jiangmiao/node-curl.

免责声明:我是作者,它是另一个模块的修改版本:https: //github.com/jiangmiao/node-curl