對于我想使用的翻譯工具,我需要按 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 is a test&target_lang=de
在 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 ($result === FALSE) { /* Handle error */ }
var_dump($result);
任何幫助將不勝感激
uj5u.com熱心網友回復:
您可以在 PHP 中使用 curl 執行 POST 請求。
// initiate the curl request
$request = curl_init();
curl_setopt($request, CURLOPT_URL,"http://www.otherDomain.com/getdata");
curl_setopt($request, CURLOPT_POST, 1);
curl_setopt($request, CURLOPT_POSTFIELDS,
"var1=value1&var2=value2");
// catch the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($request);
curl_close ($request);
// do processing for the $response
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/312522.html
上一篇:使用Perl使用基本身份驗證對RESTAPI執行POST請求
下一篇:獲取資料時顯示加載反應
