Postmanは、特定の API 呼び出しを正常に行うために使用できる API テスト インターフェイスです。CURLを使用してPHPで同じことをしようとしていますが、APIがフォーマットを期待する方法に問題があります...
私の URL が /post だとしましょう。/post の後の唯一の変数は「auth=asdf1234etc」で、次に XML データです。だから私が入れた郵便配達員を使って
/post?auth=asdf1234etc
これは URL セクションにあり、生の XML データ ポスト セクションがあり、ここに My XML を置きます。
<?xml version="1.0" encoding="UTF-8"?><usageReports><usageReport></usageReport></usageReports>
私の問題は、XML が変数ではないことです。ここで、asdf1234etc はキー auth の値であり、私の XML はキーのない値であるため、CURL を使用して投稿する方法がわかりません。
動作しない私の PHP は次のとおりです。プログラムは ?auth="..." の後のすべてが認証トークンであると見なすため、401 無許可になります。ただし、& を認証トークンの後に配置しても機能しません。これは、& の後にキーがない場所で key=value を実行する必要があるためです。
$URL = "/post?auth=asdf1234etc";
$usage_report = '<?xml version="1.0" encoding="UTF-8"?><usageReports><usageReport></usageReport></usageReports>';
$fields = array("Host" => "/post",
"Content-type" => "application/xml",
"Content-length" => strlen($usage_report),
"data" => $usage_report);//note that my xml is not a variable and I don't expect this specific line to work.
$fields_string = "&";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $fields);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$array_data = json_decode(json_encode(simplexml_load_string($result)), true);
//view response
echo "\n\n";
print_r($array_data);
echo "\n";
本当に役立つのは、郵便配達員がすべてを含むリテラル URL である出力を持っている場合、URL ベース + authtoken + XML を投稿することです。同じことをする。
私は CURLOPT_VERBOSE をオンにしました。ここに役立つ情報がいくつかあります。
POST /post?asdf123etc HTTP/1.1
Host: hidden.domain.com
Accept: */*
Content-Length: 335 //Why 335? strlen($usage_report) is 248?
Content-Type: application/x-www-form-urlencoded //why is it changing from application/xml to this?
-it’s not a certificate issue
-it’s not a connection to host issue
-it’s kindof an auth issue, because it thinks that everything after ? is the auth token
-it’s an interesting problem because I can’t set the xml to a key in the fields array
-the xml constructed and urls I use work when using postman
-not an issue of xml format
ps 重複を見つけたと思われる場合は、httpheader ではない XML を POST するときに何をすべきかについても尋ねていることを確認してください。(このようなものではありません)