avalara.net の API を使用して消費税データを取得しようとしています。その API ドキュメントには、cURL 経由で API に接続するための JSON 形式の長い文字列を含む 1 行のコードのように見えるものがリストされています。私はこの単一の文字列スタイルに精通しておらず、php マニュアルのスタイルに従う形式に変換しようとしています。たとえば、-u や -d などのインライン コマンドの代わりに curl_setopt() を介してオプションを設定しますどこにでも行く。
私はすでにphpを見ました-phpにcurlコマンドラインを実装し、試してみましたがエラーが発生し、解決策を追加しました-エラーメッセージが表示されなかったため、問題が同じかどうかはわかりませんが、そうではないように見えましたCURLOPT_FOLLOWLOCATION を追加するのは面倒です。違いはないようでした。
また、php コードの convert curl line コマンドも調べましたが、コードに既に curl_exec() があり、php のマニュアル ページを読んでいたため、役に立ちませんでした。
それらのコードはhttps://github.com/avadev/AvaTax-Calc-REST-cURL/blob/master/tax-get-POST.txtで見ることができ、関連する部分は次のとおりです。
curl -u <AccountNumber>:<LicenseKey> -H "Content-Type: text/json" -d '{
--long string of data omitted; see link above for full data--
}' "https://development.avalara.net/1.0/tax/get"
私はいくつかの調査を行い、 -u はユーザー名/パスワード用であり、 -H は特別なヘッダーであり、 -d は送信されるデータ用であることがわかりました...だから、これが私がまとめたものです:
// Identify the target URL
$url = 'https://development.avalara.net/1.0/tax/get';
// Start the process
$curl = curl_init($url);
// Tell cURL to fail if an error occurs
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
// Allow for redirects; we don't know how avalara might route requests
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
// Assign the returned data to a variable
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Set the timeout
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
// Make sure to use POST method
curl_setopt($curl, CURLOPT_POST, 1);
// Set cURL to use a login:password for access
curl_setopt($curl, CURLOPT_USERPWD, "[1100033004]:[1FC8AED1543C699B]");
// Add some custom header info
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: text/json'));
// Use Heredoc syntax to encapsulate data, to avoid having to escape a million quotes
$data = <<<EOD
{
"DocDate": "2013-12-23",
"CustomerCode": "12345678123456781234567812345678",
"CompanyCode": "EGU",
"DocType": "SalesInvoice",
"Commit": false,
"Client": "Cool ERP,3,5",
"DocCode": "29",
"DetailLevel": "Tax",
"CustomerUsageType": "G",
"ExemptionNo": "12334",
"Discount": 0,
"PurchaseOrderNo":"PO 23423",
"ReferenceCode":"",
"PosLaneCode":"",
"BusinessIdentificationNo":"",
"TaxOverride":
{
"Reason":"Item Returned",
"TaxDate":"2013-05-05",
"TaxOverrideType":"TaxDate"
},
"Addresses":
[
{
"AddressCode": "Origin",
"Line1": "269",
"Line2": "7723 Tylers Place Blvd",
"City": "West Chester",
"Region": "OH",
"PostalCode": "45069-4684",
"Country": "US"
},
{
"AddressCode": "Dest",
"Line1": "1060 W. Addison St",
"City": "Chicago",
"Region": "IL",
"PostalCode": "60613-4566",
"Country": "US"
}
],
"Lines":
[
{
"LineNo": "00001",
"DestinationCode": "Dest",
"OriginCode": "Origin",
"ItemCode": "SP-001",
"Description": "Eyeglasses",
"TaxCode": "PC030147",
"Qty": 1,
"Amount": 100
}
]
}
EOD;
// That EOD ends the encapsulation via Heredoc syntax
// Note that the actual code does not indent the closing of Heredoc; only indented for stack overflow code view
// Set the POST data
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
// Execute the transaction
$r = curl_exec($curl);
// Close the connection
curl_close($curl);
// Print the results for debugging
print_r($r);
しかし、Web ページを表示しようとすると、エラー メッセージも結果も何も表示されません。
ここのstackoverflowとphpマニュアルの他の投稿をいくつか見ました-エラーメッセージが返されないため、現時点ではデバッグする方法さえわかりません。任意の考えをいただければ幸いです。