好的,我正在使用GoToTraining API尝试创建一个新的培训。 docs说要将JSON“发布”到端点。
所以我在PHP中有我的数据数组(这是一个WordPress网站,在这里我从高级自定义字段数据中获取东西)
$my_data_array = array(
'name' => get_the_title( $_POST['acf']['field_5ab508b853122'] ),
'description' => $_POST['acf']['field_5ab50b2406457'],
'timeZone' => 'America/Los_Angeles',
'times' => array(
array(
'startDate' => date( 'Y-m-d', strtotime( $_POST['acf']['field_5ab50908347ef'] ) ) . 'T' . $_POST['acf']['field_5ab50bc6af8df'] . 'Z',
'endDate' => date( 'Y-m-d', strtotime( $_POST['acf']['field_5ab50908347ef'] ) ) . 'T' . $_POST['acf']['field_5abbd96c6ff8d'] . 'Z',
),
),
'registrationSettings' => array(
'disableConfirmationEmail' => false,
'disableWebRegistration' => true,
)
);
然后我就是这样做:
$payload = json_encode( $my_data_array )
创建有效负载,看起来像这样,如果我print_r $ payload的值:
{"name":"Today’s Class","description":"We can add some default content here","timeZone":"America\/Los_Angeles","times":[{"startDate":"2018-04-24T06:00:00Z","endDate":"2018-04-24T09:00:00Z"}],"registrationSettings":{"disableConfirmationEmail":false,"disableWebRegistration":true}}
如果我发出curl请求,发布$ payload,我会回来:
Array
(
[errorCode] => InternalError
[description] => We have encountered an internal error. The request may
be retried, but it may have the same result.
[incident] => 5102160953111715072
)
可能有很多东西,对吧?但是这件事让我发疯了:
如果将上面有效负载转储的值剪切并粘贴到API参考页面上沙盒中的“body”字段中(请参阅上面的链接),它可以正常工作,我将获得新训练的ID作为回应。
此外,如果我剪切并粘贴相同的字符串,并在我的脚本中将其硬编码为my $ payload变量的字符串值(使用完全相同的curl内容代码) - 唯一不同的是我正在硬编码$ payload的值,像这样
$payload = '{"name":"Today’s Class","description":"We can add some default content here","timeZone":"America\/Los_Angeles","times":[{"startDate":"2018-04-24T06:00:00Z","endDate":"2018-04-24T09:00:00Z"}],"registrationSettings":{"disableConfirmationEmail":false,"disableWebRegistration":true}}';
...并以这种方式提出请求,它也有效并给我一个成功的回应!
protected function make_request( $url, $method, $fields ) {
// $payload = json_encode( $fields );
$payload = '{"name":"Getting to Know Today’s Electric Utility Industry","description":"We can add some default content here","timeZone":"America\/Los_Angeles","times":[{"startDate":"2018-04-24T06:00:00Z","endDate":"2018-04-24T09:00:00Z"}],"registrationSettings":{"disableConfirmationEmail":false,"disableWebRegistration":true}}';
$http_headers = array(
'Accept: application/json',
'Content-Type: application/json',
'Authorization: OAuth oauth_token=' . $this->access_token,
);
$ch = curl_init();
if ( 'post' == $method ) {
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload ); // define what you want to post
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // return the output in string format
curl_setopt( $ch, CURLOPT_HTTPHEADER, $http_headers );
}
$response = curl_exec( $ch );
curl_close( $ch ); // close curl handle
return $response;
}
但是如果我print_r json_encode()返回的内容,那么分配给$ payload的内容与我得到的完全相同。 Whaa ??
我注意到,如果我跑
mb_detect_encoding( $payload );
我从json_encode版本获得'ASCII',并且在我将字符串硬编码为变量值的版本上得到'UTF-8'。这有什么不同吗?
我错过了一些简单的东西吗?任何人都知道我在这里做错了什么?我的头靠墙撞了好几个小时。
答案 0 :(得分:0)
因此对于后代:原来,训练名称中的撇号导致GTT阻止发送的数据。但它与get_the_title()函数有关,它处理撇号和引号的方式不同于只从$ post-> post_title中获取值。当我将用于API调用的数据的代码更改为$ post-> post_title而不是get_the_title()时,它可以正常工作。