gpt4 book ai didi

php - 如何使用 curl_setopt() 将提供的带有内联参数的单行 cURL 代码转换为格式

转载 作者:搜寻专家 更新时间:2023-10-31 21:10:33 25 4
gpt4 key购买 nike

我正在尝试使用来自 avalara.net 的 API 来获取销售税数据,他们的 API 文档列出了看起来本质上是一行代码,其中包含一个 JSON 格式的长字符串,用于通过 cURL 连接到他们的 API;我不熟悉这种单一的字符串样式,并试图将其转换为遵循 php 手册样式的格式,例如通过 curl_setopt() 而不是像 -u 和 -d 这样的内联命令来设置选项,但我不是到达任何地方。

我已经看过 php - implement curl command line to php, tried but getting error 并添加了他们的解决方案——因为我没有收到任何错误消息,我不知道我的问题是否相同,但添加 CURLOPT_FOLLOWLOCATION 似乎不会有什么坏处。似乎没有任何区别。

我也查看了 convert curl line command in php code,但它没有帮助,因为我的代码中已经有 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 是一个特殊的 header ,-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);

但是,当我尝试查看网页时,我什么也没看到 - 没有错误消息,没有结果,什么都没有。

我已经查看了 stackoverflow 和 php 手册中的其他一些帖子 - 我现在不确定如何调试它,因为我没有收到任何错误消息。如有任何想法,我们将不胜感激。

最佳答案

这是一个演示在 PHP 中使用 API 的示例,这可能会帮助您将一些数据发布到 API

$url = '<your url here>';
$curl = curl_init($url);

$data_string = '[
{
"FirstName": "Value"
},
{
"EmailAddress": "Value"
},
{
"Phone": "Value"
}
]';

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'Content-Length:'.strlen($data_string)

));

$json_response = curl_exec($curl);
$curl_errorno = curl_errno($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ($status) {
echo 'Status is 200 OK! ';
}else{
echo "Sorry Something went wrong. Please retry!";
curl_close($curl);
}

弄清楚您在状态代码中获得的内容对您有帮助。编辑

这将是你的值(value)

$data_string=
'{
"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
}
]
}'

回答像这样去掉大括号形成这条线

curl_setopt($curl, CURLOPT_USERPWD, "1100033004:1FC8AED1543C699B");

关于php - 如何使用 curl_setopt() 将提供的带有内联参数的单行 cURL 代码转换为格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20742311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com