php-curl.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { isJSONContentType } from "~/helpers/utils/contenttypes"
  2. export const PhpCurlCodegen = {
  3. id: "php-curl",
  4. name: "PHP cURL",
  5. generator: ({
  6. url,
  7. pathName,
  8. queryString,
  9. auth,
  10. httpUser,
  11. httpPassword,
  12. bearerToken,
  13. method,
  14. rawInput,
  15. rawParams,
  16. rawRequestBody,
  17. contentType,
  18. headers,
  19. }) => {
  20. const requestString = []
  21. let genHeaders = []
  22. requestString.push(`<?php\n`)
  23. requestString.push(`$curl = curl_init();\n`)
  24. requestString.push(`curl_setopt_array($curl, array(\n`)
  25. requestString.push(` CURLOPT_URL => "${url}${pathName}${queryString}",\n`)
  26. requestString.push(` CURLOPT_RETURNTRANSFER => true,\n`)
  27. requestString.push(` CURLOPT_ENCODING => "",\n`)
  28. requestString.push(` CURLOPT_MAXREDIRS => 10,\n`)
  29. requestString.push(` CURLOPT_TIMEOUT => 0,\n`)
  30. requestString.push(` CURLOPT_FOLLOWLOCATION => true,\n`)
  31. requestString.push(` CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,\n`)
  32. requestString.push(` CURLOPT_CUSTOMREQUEST => "${method}",\n`)
  33. if (auth === "Basic Auth") {
  34. const basic = `${httpUser}:${httpPassword}`
  35. genHeaders.push(
  36. ` "Authorization: Basic ${window.btoa(unescape(encodeURIComponent(basic)))}",\n`
  37. )
  38. } else if (auth === "Bearer Token" || auth === "OAuth 2.0") {
  39. genHeaders.push(` "Authorization: Bearer ${bearerToken}",\n`)
  40. }
  41. if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
  42. let requestBody = rawInput ? rawParams : rawRequestBody
  43. if (
  44. !isJSONContentType(contentType) &&
  45. rawInput &&
  46. !contentType.includes("x-www-form-urlencoded")
  47. ) {
  48. const toRemove = /[\n {}]/gim
  49. const toReplace = /:/gim
  50. const parts = requestBody.replace(toRemove, "").replace(toReplace, "=>")
  51. requestBody = `array(${parts})`
  52. } else if (isJSONContentType(contentType)) {
  53. requestBody = JSON.stringify(requestBody)
  54. } else if (contentType.includes("x-www-form-urlencoded")) {
  55. if (requestBody.includes("=")) {
  56. requestBody = `"${requestBody}"`
  57. } else {
  58. const requestObject = JSON.parse(requestBody)
  59. requestBody = `"${Object.keys(requestObject)
  60. .map((key) => `${key}=${requestObject[key].toString()}`)
  61. .join("&")}"`
  62. }
  63. }
  64. if (contentType) {
  65. genHeaders.push(` "Content-Type: ${contentType}; charset=utf-8",\n`)
  66. }
  67. requestString.push(` CURLOPT_POSTFIELDS => ${requestBody},\n`)
  68. }
  69. if (headers.length > 0) {
  70. headers.forEach(({ key, value }) => {
  71. if (key) genHeaders.push(` "${key}: ${value}",\n`)
  72. })
  73. }
  74. if (genHeaders.length > 0 || headers.length > 0) {
  75. requestString.push(
  76. ` CURLOPT_HTTPHEADER => array(\n${genHeaders.join("").slice(0, -2)}\n )\n`
  77. )
  78. }
  79. requestString.push(`));\n`)
  80. requestString.push(`$response = curl_exec($curl);\n`)
  81. requestString.push(`curl_close($curl);\n`)
  82. requestString.push(`echo $response;\n`)
  83. return requestString.join("")
  84. },
  85. }