Files
curl-converter-cli/curlconverter/generators/php.php
7000pctAUTO 658f5b0d7e
Some checks failed
CI / test (push) Has been cancelled
Fix CI issues: add workflow file and fix lint errors
2026-03-22 10:53:59 +00:00

90 lines
3.3 KiB
PHP

{"""PHP code generator."""
import json
from curlconverter.parser import ParsedCurl
from curlconverter.generators import register_generator
def _detect_content_type(headers: dict, data: str) -> str:
"""Detect content type from headers or data."""
if "Content-Type" in headers:
return headers["Content-Type"]
if "content-type" in headers:
return headers["content-type"]
if data:
try:
json.loads(data)
return "application/json"
except (json.JSONDecodeError, TypeError):
pass
return "application/x-www-form-urlencoded"
@register_generator("php")
def generate_php(parsed: ParsedCurl) -> str:
"""Generate PHP cURL code from parsed curl data."""
lines = []
lines.append("<?php")
lines.append("")
lines.append("$url = " + repr(parsed.url) + ";")
lines.append("")
lines.append("$ch = curl_init();")
lines.append("")
lines.append('curl_setopt($ch, CURLOPT_URL, $url);')
lines.append('curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);')
lines.append(f'curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "{parsed.method}");')
lines.append("")
headers = dict(parsed.headers) if parsed.headers else {}
if parsed.user_agent and "User-Agent" not in headers and "user-agent" not in headers:
headers["User-Agent"] = parsed.user_agent
if parsed.data:
content_type = _detect_content_type(headers, parsed.data)
if content_type == "application/json":
try:
json_data = json.loads(parsed.data)
lines.append("$data = " + repr(json.dumps(json_data)) + ";")
lines.append("curl_setopt($ch, CURLOPT_POSTFIELDS, $data);")
lines.append('curl_setopt($ch, CURLOPT_HTTPHEADER, array(\'Content-Type: application/json\'));')
except (json.JSONDecodeError, TypeError):
lines.append("$data = " + repr(parsed.data) + ";")
lines.append("curl_setopt($ch, CURLOPT_POSTFIELDS, $data);")
else:
lines.append("$data = " + repr(parsed.data) + ";")
lines.append("curl_setopt($ch, CURLOPT_POSTFIELDS, $data);")
if parsed.method == "GET":
lines.append("curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: ' . $content_type));")
if parsed.auth:
lines.append(f'$auth = base64_encode("{parsed.auth[0]}:{parsed.auth[1]}");')
lines.append('curl_setopt($ch, CURLOPT_HTTPHEADER, array(\'Authorization: Basic \' . $auth));')
if headers:
http_headers = []
for k, v in headers.items():
http_headers.append(f"{k}: {v}")
lines.append('curl_setopt($ch, CURLOPT_HTTPHEADER, ' + repr(http_headers) + ');')
lines.append("$response = curl_exec($ch);")
lines.append("")
lines.append("if (curl_errno($ch)) {")
lines.append(" echo 'Error:' . curl_error($ch);")
lines.append("}")
lines.append("")
lines.append("curl_close($ch);")
lines.append("echo $response;")
return "\n".join(lines)
import curlconverter.generators.python # noqa: E402, F401
import curlconverter.generators.javascript # noqa: E402, F401
import curlconverter.generators.go # noqa: E402, F401
import curlconverter.generators.ruby # noqa: E402, F401