php-curl.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 (
  47. !isJSONContentType(contentType) &&
  48. rawInput &&
  49. !contentType.includes("x-www-form-urlencoded")
  50. ) {
  51. const toRemove = /[\n {}]/gim
  52. const toReplace = /:/gim
  53. const parts = requestBody.replace(toRemove, "").replace(toReplace, "=>")
  54. requestBody = `array(${parts})`
  55. } else if (isJSONContentType(contentType)) {
  56. requestBody = JSON.stringify(requestBody)
  57. } else if (contentType.includes("x-www-form-urlencoded")) {
  58. if (requestBody.includes("=")) {
  59. requestBody = `"${requestBody}"`
  60. } else {
  61. const requestObject = JSON.parse(requestBody)
  62. requestBody = `"${Object.keys(requestObject)
  63. .map((key) => `${key}=${requestObject[key]}`)
  64. .join("&")}"`
  65. }
  66. }
  67. if (contentType) {
  68. genHeaders.push(` "Content-Type: ${contentType}; charset=utf-8",\n`)
  69. }
  70. requestString.push(` CURLOPT_POSTFIELDS => ${requestBody},\n`)
  71. }
  72. if (headers.length > 0) {
  73. headers.forEach(({ key, value }) => {
  74. if (key) genHeaders.push(` "${key}: ${value}",\n`)
  75. })
  76. }
  77. if (genHeaders.length > 0 || headers.length > 0) {
  78. requestString.push(
  79. ` CURLOPT_HTTPHEADER => array(\n${genHeaders
  80. .join("")
  81. .slice(0, -2)}\n )\n`
  82. )
  83. }
  84. requestString.push(`));\n`)
  85. requestString.push(`$response = curl_exec($curl);\n`)
  86. requestString.push(`curl_close($curl);\n`)
  87. requestString.push(`echo $response;\n`)
  88. return requestString.join("")
  89. },
  90. }