Php 如何使用cURL_setopt()将提供的带有内联参数的一行cURL代码转换为一种格式

Php 如何使用cURL_setopt()将提供的带有内联参数的一行cURL代码转换为一种格式,php,json,api,curl,avalara,Php,Json,Api,Curl,Avalara,我试图使用avalara.net的API获取销售税数据,他们的API文档列出了一行代码,其中包含一个JSON格式的长字符串,用于通过cURL连接到他们的API;我不熟悉这种单字符串样式,我正在尝试将其转换为一种遵循php手册样式的格式,例如,通过curl_setopt()设置选项,而不是像-u和-d这样的内联命令,但我没有任何进展 我已经查看并添加了他们的解决方案-因为我没有收到任何错误消息,我不知道我的问题是否相同,但添加CURLOPT_FOLLOWLOCATION似乎没有什么坏处。似乎没什么

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

我已经查看并添加了他们的解决方案-因为我没有收到任何错误消息,我不知道我的问题是否相同,但添加CURLOPT_FOLLOWLOCATION似乎没有什么坏处。似乎没什么区别

我也看了一下,但没有帮助,因为我的代码中已经有curl_exec(),并且一直在阅读php手册页面

他们的代码可在以下相关部分查看:

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"
curl-u:-H“内容类型:text/json”-d”{
--省略了数据的长字符串;有关完整数据,请参见上面的链接--
}' "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);
//标识目标URL
$url='1https://development.avalara.net/1.0/tax/get';
//开始这个过程
$curl=curl\u init($url);
//如果发生错误,告诉cURL失败
curl_setopt($curl,CURLOPT_FAILONERROR,1);
//允许重定向;我们不知道avalara会如何路由请求
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
//将返回的数据分配给变量
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
//设置超时
curl_setopt($curl,CURLOPT_超时,5);
//确保使用POST方法
curl_setopt($curl,CURLOPT_POST,1);
//将cURL设置为使用登录:密码进行访问
curl_setopt($curl,CURLOPT_USERPWD,“[1100033004]:[1FC8AED1543C699B]”);
//添加一些自定义标题信息
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-type:text/json'));
//使用herdoc语法封装数据,以避免必须转义一百万个引号

$data=这是一个演示如何在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);
    }
回答 将支架从这条直线上拆下,如下所示

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

您在REST(Mozilla中的插件)中使用过api吗?您可以在那里调试并获取响应头并找出发生了什么?我不知道有这样一个插件-我将立即添加它并开始查看。不要忘记添加自定义头
Content-Type:application/json
Name是Content-Type,Value是application/json。有趣的插件。我添加了一个自定义头和基本身份验证—在我这么做之前,我从他们的服务器上得到了一些错误—但现在我在该工具中没有看到任何响应头或主体—一个都没有?设置为POST方法,URL,标题为内容类型:text/json,身份验证设置为通过电子邮件提供的用户名/许可证密钥。。现在根本没有响应。您必须获得一些响应头代码,检查出manAdded curl_setopt($curl,CURLOPT_USERPWD,“[xxxxx]:[xxxxx]”);使用提供的凭据,仍将获得400状态。这意味着您发送了错误的数据。请使用来验证您的json数据。您还可以在urljsonlint中添加访问密钥和密码,并将其报告为有效。当您使用REST时,使用以下详细信息,如Url:,
内容类型:application/json
,method=
POST
,然后他们会在给出您收到的消息后询问一些用户名和密码?请告诉我响应标题消息。别忘了把json数据放在正文中。
curl_setopt($curl, CURLOPT_USERPWD, "1100033004:1FC8AED1543C699B");