如何将cURL PUT语法转换为正确的PHP

时间:2018-01-03 05:51:01

标签: php api curl put

我很难让Qualtrics PUT API使用PHP。

cURL语法是:

    curl -X PUT -H 'X-API-TOKEN: yourtokenhere'  -H 'Content-Type: application/json' -d '{
    "status": "active",
    "lastName": "ExampleLastName",
    "password": "uwillneverguess",
    "firstName": "ExampleFirstName",
    "userType": "UT_1234567890AbCdE",
    "accountExpirationDate": null,
    "permissions": {
        "controlPanel": {
            "accountPermissions": {
                "accessApi": {
                    "state": "off"
                }
            }
        }
    }
}' 'https://yourdatacenterid.qualtrics.com/API/v3/users/UR_1234567890AbCdE'

我需要这个在PHP中工作。我正在尝试更新密码。我会感激任何帮助,因为我已经尝试了一段时间但没有成功。

Qualtrics没有提供任何示例PHP代码,但以下附加信息可能有所帮助:

请求标题

Accept: */*
X-API-TOKEN: yourtokenhere
accept-encoding: gzip, deflate
content-type: application/json
content-length: 24

请求数据

{
    "password": "Password1"
}

提前致谢。

2 个答案:

答案 0 :(得分:1)

以下是基于curl示例的示例php示例。

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://yourdatacenterid.qualtrics.com/API/v3/users/UR_1234567890AbCdE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n    \"status\": \"active\",\n    \"lastName\": \"ExampleLastName\",\n    \"password\": \"uwillneverguess\",\n    \"firstName\": \"ExampleFirstName\",\n    \"userType\": \"UT_1234567890AbCdE\",\n    \"accountExpirationDate\": null,\n    \"permissions\": {\n        \"controlPanel\": {\n            \"accountPermissions\": {\n                \"accessApi\": {\n                    \"state\": \"off\"\n                }\n            }\n        }\n    }\n}");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");


$headers = array();
$headers[] = "X-Api-Token: yourtokenhere";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

答案 1 :(得分:0)

感谢Abhinav Verma的回答:

$ch = curl_init();

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, "https://yourdatacenterid.qualtrics.com/API/v3/users/UR_1234567890AbCdE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n    \"status\": \"active\",\n    \"lastName\": \"ExampleLastName\",\n    \"password\": \"uwillneverguess\",\n    \"firstName\": \"ExampleFirstName\",\n    \"userType\": \"UT_1234567890AbCdE\",\n    \"accountExpirationDate\": null,\n    \"permissions\": {\n        \"controlPanel\": {\n            \"accountPermissions\": {\n                \"accessApi\": {\n                    \"state\": \"off\"\n                }\n            }\n        }\n    }\n}");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");


$headers = array();
$headers[] = "X-Api-Token: yourtokenhere";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);