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

问题描述 投票:-3回答:2

我很难让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"
}

提前致谢。

php api curl put
2个回答
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);

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);
© www.soinside.com 2019 - 2024. All rights reserved.