php-curl.js 3.1 KB

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