对于我想要使用的翻译工具,我需要按 POST 发送我的请求。我从来没有做过这样的事情,文档对我来说意义不大(它不是 php 特定的文档,也不知道如何在 php 中实现它)
文档中的示例是:
POST /v2/translate HTTP/1.0
Host: api.deepl.com
Accept: */*
User-Agent: YourApp
Content-Type: application/x-www-form-urlencoded
Content-Length: 91
auth_key=MYKEY&text=this%20is%20a%20test&target_lang=de
Run Code Online (Sandbox Code Playgroud)
在 stackoverflow 上的另一篇文章中,我发现这个解决方案可以在 php 中使用 POST,所以我的主要问题是,我不确定所有内容都去哪里了,我假设主机进入 url 并且该标头是正确的。但这就是我以我有限的技能所能达到的程度
$url = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if …Run Code Online (Sandbox Code Playgroud)