点击按钮后用PHP向Azure DevOps提交JSON数据报错求助
问题描述
我正尝试向Azure DevOps提交JSON数据创建变量组,已编写并验证可用的PowerShell代码,但编写的PHP代码运行时出现致命错误。
可用的PowerShell代码
$organization = "DevOpsOrg" $project = "Worldhealthgroup" $url_base = "https://dev.azure.com/$organization/$project/" $url_endpoint = "_apis/distributedtask/variablegroups?api-version=6.0-preview.2" $url = $url_base + $url_endpoint $pat = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" $token =[System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($pat)")) $JSON = @' { "description": "TestVariableGroup", "name": "TestVariableGroup", "providerData": null, "type": "Vsts", "variables": {"applicationID": { "isSecret": false, "value": "xxxxxx-xxxx-xxxx-xxxx-75cf76fc40c8" }, "ResourceGroupName": { "isSecret": false, "value": "TestRG1" }, "VirtualMachineName": { "isSecret": false, "value": "TestVM1" }, "imagesize": { "isSecret": false, "value": "Standard_B2ms" }, "location": { "isSecret": false, "value": "eastus" }, "imageVnet": { "isSecret": false, "value": "LocalVnet" }, "imageSubnet": { "isSecret": false, "value": "Subnet1" }, "imagestorageRg": { "isSecret": false, "value": "Img_rg" }}, "variableGroupProjectReferences": [{ "description": "TestVariableGroup", "name": "TestVariableGroup", "projectReference": { "id": "projectId", "name": "Worldhealthgroup" } }] } '@ $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON
出错的PHP代码
<?php if(isset($_POST['btnsubmit'])) { $organization = "DevOpsOrg" $project = "Worldhealthgroup" $url_base = "https://dev.azure.com/$organization/$project/" $url_endpoint = "_apis/distributedtask/variablegroups?api-version=6.0-preview.2" $url = $url_base.$url_endpoint; $pattoken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; $ci = curl_init(); curl_setopt($ci, CURLOPT_URL, $url); curl_setopt($ci, CURLOPT_RETURNTRANSFER, true); curl_setopt($ci, CURLOPT_ENCODING, ''); curl_setopt($ci, CURLOPT_TIMEOUT, 0); curl_setopt($ci, CURLOPT_MAXREDIRS, 10 ); curl_setopt($ci, CURLOPT_FOLLOWLOCATION, true ); curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false); curl_setpot($ci, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setpot($ci, CURLOPT_POSTFIELDS, '{ "description": "TestVariableGroup", "name": "TestVariableGroup", "providerData": null, "type": "Vsts", "variables": {"applicationID": { "isSecret": false, "value": "xxxxxx-xxxx-xxxx-xxxx-75cf76fc40c8" }, "ResourceGroupName": { "isSecret": false, "value": "TestRG1" }, "VirtualMachineName": { "isSecret": false, "value": "TestVM1" }, "imagesize": { "isSecret": false, "value": "Standard_B2ms" }, "location": { "isSecret": false, "value": "eastus" }, "imageVnet": { "isSecret": false, "value": "LocalVnet" }, "imageSubnet": { "isSecret": false, "value": "Subnet1" }, "imagestorageRg": { "isSecret": false, "value": "Img_rg" }}, "variableGroupProjectReferences": [{ "description": "TestVariableGroup", "name": "TestVariableGroup", "projectReference": { "id": "projectId", "name": "Worldhealthgroup" } }] }' ); curl_setopt($ci, CURLOPT_HTTPHEADER, array( "content-type: application/json", "content-length: 0", "Authorization: Basic ".base64_encode(":" . $pattoken) // This is the best way to convert the best 64 string in PHP ) ); } ?> <form action="" method="post" name ="formimagecatalog" enctype="multipart/form-data"> <div class="col-md-6"> <div class="form-group"> <div class="box-footer"> <button type="submit" class="btn btn-info" name="btnsubmit">Submit</button> </div> </form>
报错信息
Fatal error: Uncaught Error: Call to undefined function curl_setpot() in C:\xampp\htdocs\restapi2\devopspost.php
解决步骤
- 修正函数拼写错误:将代码中两处
curl_setpot改为正确的curl_setopt,这是导致致命错误的直接原因。 - 补充语句结束符:PHP每条语句必须以分号结尾,给前几行变量赋值添加分号:
$organization = "DevOpsOrg"; $project = "Worldhealthgroup"; $url_base = "https://dev.azure.com/$organization/$project/"; $url_endpoint = "_apis/distributedtask/variablegroups?api-version=6.0-preview.2"; - 移除错误请求头:删除
content-length: 0这个请求头,否则Azure DevOps无法正确解析提交的JSON内容。 - 完善请求执行流程:添加curl执行和关闭代码,同时增加错误处理:
$response = curl_exec($ci); if(curl_errno($ci)) { echo '请求错误: ' . curl_error($ci); } else { echo $response; } curl_close($ci);
内容的提问来源于stack exchange,提问作者askwizard
相关产品推荐
相关产品推荐

