1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | 1 1 9 9 9 9 9 9 9 9 9 9 9 81 68 9 4 9 2 9 8 9 6 9 9 9 9 9 9 9 9 1 | 'use strict' var util = require('util') module.exports = function (source, options) { var opts = util._extend({ indent: ' ', noTags: false, maxRedirects: 10, timeout: 30, closingTag: false }, options) var code = [] Eif (!opts.noTags) { code.push('<?php') code.push('') } code.push('$curl = curl_init();') code.push('') var curlOptions = [{ escape: true, name: 'CURLOPT_PORT', value: source.uriObj.port }, { escape: true, name: 'CURLOPT_URL', value: source.fullUrl }, { escape: false, name: 'CURLOPT_RETURNTRANSFER', value: 'true' }, { escape: true, name: 'CURLOPT_ENCODING', value: '' }, { escape: false, name: 'CURLOPT_MAXREDIRS', value: opts.maxRedirects }, { escape: false, name: 'CURLOPT_TIMEOUT', value: opts.timeout }, { escape: false, name: 'CURLOPT_HTTP_VERSION', value: source.httpVersion === 'HTTP/1.0' ? 'CURL_HTTP_VERSION_1_0' : 'CURL_HTTP_VERSION_1_1' }, { escape: true, name: 'CURLOPT_CUSTOMREQUEST', value: source.method }, { escape: true, name: 'CURLOPT_POSTFIELDS', value: source.postData ? source.postData.text : undefined }] code.push('curl_setopt_array($curl, array(') var curlopts = [] curlOptions.map(function (option) { if (!~[null, undefined].indexOf(option.value)) { curlopts.push(util.format('%s => %s,', option.name, option.escape ? JSON.stringify(option.value) : option.value)) } }) // construct cookies var cookies = source.cookies.map(function (cookie) { return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value) }) if (cookies.length) { curlopts.push(util.format('CURLOPT_COOKIE => "%s",', cookies.join('; '))) } // construct cookies var headers = Object.keys(source.headersObj).sort().map(function (key) { return util.format('"%s: %s"', key, source.headersObj[key]) }) if (headers.length) { curlopts.push(util.format('CURLOPT_HTTPHEADER => array(\n%s%s%s\n%s),', opts.indent, opts.indent, headers.join(',\n' + opts.indent + opts.indent), opts.indent)) } code.push(opts.indent + curlopts.join('\n' + opts.indent)) code.push('));') code.push('') code.push('$response = curl_exec($curl);') code.push('') code.push('curl_close($curl);') Iif (opts.closingTag) { code.push('?>') } return code.join('\n') } module.exports.info = { key: 'curl', title: 'cURL', link: 'http://php.net/manual/en/book.curl.php', description: 'PHP with libcurl' } |