| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- import { Fence } from '@/components/Fence'
- const PhpRequestRenderer = ({
- url,
- headers,
- bodyMethod,
- rpcVersion,
- bodyParams,
- id,
- }) => {
- const httpBody = bodyParams
- // Handle default values for `rpcVersion` and `id`
- const rpcVersionValue = rpcVersion || '2.0'
- const idValue = id || 1
- // Convert httpBody to a PHP-compatible format
- const formatParamsForPhp = (params) => {
- if (Array.isArray(params)) {
- return `[${params.map((item) => `'${item}'`).join(', ')}]` // Correct array formatting for PHP
- }
- if (typeof params === 'object') {
- return `[${Object.entries(params)
- .map(([key, value]) => `'${key}' => '${value}'`)
- .join(', ')}]` // Use PHP array syntax for key-value pairs
- }
- return `'${params}'`
- }
- const formattedParams = formatParamsForPhp(httpBody)
- const headersCode = Object.entries(headers)
- .map(([key, value]) => `"${key}: ${value}"`)
- .join(',\n ')
- const code = `
- <?php
- $url = "${url}";
- $data = [
- "jsonrpc" => "${rpcVersionValue}",
- "id" => ${idValue},
- "method" => "${bodyMethod}",
- "params" => ${formattedParams}
- ];
- // Initialize cURL session
- $ch = curl_init($url);
- // Set cURL options
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
- curl_setopt($ch, CURLOPT_HTTPHEADER, [
- "Content-Type: application/json",
- ]);
- // Execute cURL request
- $response = curl_exec($ch);
- // Check for errors
- if ($response === false) {
- echo "cURL Error: " . curl_error($ch);
- } else {
- echo "Response: " . $response;
- }
- // Close cURL session
- curl_close($ch);
- ?>
- `
- return (
- <Fence className="w-full" language="php">
- {code}
- </Fence>
- )
- }
- export default PhpRequestRenderer
|