99 lines
3.3 KiB
Python
99 lines
3.3 KiB
Python
"""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(f'curl_setopt($ch, CURLOPT_URL, $url);')
|
|
lines.append(f'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 = " + json.dumps(json_data) + ";")
|
|
lines.append("curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));")
|
|
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 "Content-Type" not in headers and "content-type" not in headers:
|
|
headers["Content-Type"] = content_type
|
|
|
|
lines.append("")
|
|
|
|
if headers:
|
|
curl_headers = []
|
|
for k, v in headers.items():
|
|
curl_headers.append(f"{k}: {v}")
|
|
lines.append("$headers = " + repr(curl_headers) + ";")
|
|
lines.append("curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);")
|
|
lines.append("")
|
|
|
|
if parsed.auth:
|
|
lines.append(f'curl_setopt($ch, CURLOPT_USERPWD, "{parsed.auth[0]}:{parsed.auth[1]}");')
|
|
lines.append("")
|
|
|
|
if parsed.cookies:
|
|
lines.append(f'curl_setopt($ch, CURLOPT_COOKIE, "{parsed.cookies}");')
|
|
lines.append("")
|
|
|
|
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("")
|
|
lines.append("echo $response;")
|
|
|
|
return "\n".join(lines)
|
|
|
|
|
|
import curlconverter.generators.python
|
|
import curlconverter.generators.javascript
|
|
import curlconverter.generators.go
|
|
import curlconverter.generators.ruby
|
|
import curlconverter.generators.php
|