Skip to content
Jackie Chen edited this page Dec 15, 2017 · 17 revisions

Curl in Node.js

Phplike support the curl function of PHP, you can easily use curl to make a GET,POST request, also get the header and cookie in response.

The following is a example to show how to use curl in Node.js .

var php = require("phplike/module.js");
var url = "http://localhost:8080/";
var param = {"type": "xxxx"}; // Send parameter
var header = {"Cookie": "xxx"}; // Send cookie
var c = php.curl_init();
php.curl_setopt(c, 'CURLOPT_URL', url);
php.curl_setopt(c, 'CURLOPT_POST', 1);
php.curl_setopt(c, 'CURLOPT_POSTFIELDS', "a=bbb&c=eee");
php.curl_setopt(c, 'CURLOPT_HTTPHEADER', header);
var res = php.curl_exec(c);
var responseHeader = php.getResponseHeader(); // Get header

Be more easy to use curl.

The name "request" is a more easier method to make a request.

var php = require('phplike/module.js');

var url = "http://localhost:8080/";
var param = {"q": "x"};
var header = {"Cookie": "xxx"};
var res = php.request("GET", url, param, header);

How to upload files

var php = require('phplike/module.js');
var url = "http://localhost/file.php";
var filePath = "/xxx/xxx/test.zip"
var param = {"fileField": "@" + filePath+ ""};
var ch = php.curl_init();
php.curl_setopt(ch, 'CURLOPT_URL', url);
php.curl_setopt(ch, 'CURLOPT_POST',1);
php.curl_setopt(ch, 'CURLOPT_POSTFIELDS', param);
var response = php.curl_exec(c);

Upload Example 2

var php = require('phplike/module.js');

var url = "http://localhost:8080/";
var param = {"q": "x"};
var header = {"Cookie": "xxx"};
var file = {"fileName": "/tmp/xx.png"};
var option = {};
var res = php.request("POST", url, param, header, option, file);

How to download binary file

var php = require('phplike/module.js');
var url = 'http://xxx.xxxs.me/xxx.docs';
var params = {}; 
var header = {}; 
var options = {"BINARY_RESPONSE": "1"};
    
var res = php.request("GET", url, params, header, options);
console.log(res); // You can see the output is a array buffer                                                                                                                            
php.file_put_contents("/tmp/xxx.docs", res, 'binary');

How to pass the ssl certification.

var php = require('phplike/module.js');
var url = 'http://xxx.xxxs.me/xxx.docs';
var params = {}; 
var header = {}; 
var options = {"CURLOPT_SSL_VERIFYHOST": 0, "CURLOPT_SSL_VERIFYPEER": 0};
    
var res = php.request("GET", url, params, header, options);
console.log(res);                                                                                                                          

SSL Version: Using tls-1.2

var php = require('phplike/module.js');
var url = 'https://xxx.xxxs.me/xxx.docs';
var params = {}; 
var header = {}; 
var options = {"CURLOPT_SSL_VERIFYHOST": 0, "CURLOPT_SSL_VERIFYPEER": 0};
options['CURLOPT_SSLVERSION'] = "CURL_SSLVERSION_TLSv1_2";
var res = php.request("GET", url, params, header, options);
console.log(res);                                                                                                                          
Clone this wiki locally