我有一个要转换为PHP的cURL命令。
curl -XPOST -H"X-Ovh-Application: 7kbG7Bk7S9Nt7ZSV" -H "Content-type: application/json" \
https://ca.api.ovh.com/1.0/auth/credential -d '{
"accessRules": [
{
"method": "GET",
"path": "/*"
}
],
"redirection":"https://www.mywebsite.com/"
}'
cURL命令返回正确的值:
{"consumerKey":"6br3ends9irOEWuBDoKHVOjNk54LEQlxya","state":"pendingValidation","validationUrl":"https://eu.api.ovh.com/auth/?credentialToken=W6lERcYmhvrSewowZglIP2ZJYJ8DsiUlFpWaVGO5UtebHjO431nOcS7UMddOgebk"}
我在PHP中尝试过的事情:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://ca.api.ovh.com/1.0/auth/credential");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n \"accessRules\": [\n {\n \"method\": \"GET\",\n \"path\": \"/*\"\n }\n ],\n \"redirection\":\"https://www.mywebsite.com/\"\n}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "X-Ovh-Application: 7kbG7Bk7S9Nt7ZSV";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
print_r($result);
脚本未返回预期值:
{"message":"Received not described parameters: ({\"accessRules\":{\"method\":\"GET\",\"path\":\"/*\"}}) while calling credential"}
也许是因为帖子数据的格式不正确?
答案 0 :(得分:0)
您必须将json对象用于数据发布。您可以使用json_encode()函数将php的数组对象转换为json数据。
您可以尝试以下代码:
$data = ['accessRules' => ['method' => 'GET', 'path' => '/*'], 'redirection' => 'https://www.mywebsite.com/'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://ca.api.ovh.com/1.0/auth/credential");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "X-Ovh-Application: 7kbG7Bk7S9Nt7ZSV";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
print_r($result);
答案 1 :(得分:0)
您也可以使用此站点,https://incarnate.github.io/curl-to-php/将从原始curl命令转换为PHP。
已更新:
从ovh文档中,您可以为PHP使用OVH api客户端,这是他们在其doc site中的示例:
<?php
/**
* First, download the latest release of PHP wrapper on github
* And include this script into the folder with extracted files
*/
require __DIR__ . '/vendor/autoload.php';
use \Ovh\Api;
/**
* Instanciate an OVH Client.
* You can generate new credentials with full access to your account on
* the token creation page
*/
$ovh = new Api( 'xxxxxxxxxx', // Application Key
'xxxxxxxxxx', // Application Secret
'ovh-eu', // Endpoint of API OVH Europe (List of available endpoints)
'xxxxxxxxxx'); // Consumer Key
$result = $ovh->post('/auth/credential', array(
'accessRules' => 'xxxx', // Required: Access required for your application (type: auth.AccessRule[])
));
print_r( $result );
答案 2 :(得分:0)
首先,这是使用php设置json的一种糟糕方法:
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n \"accessRules\": [\n {\n \"method\": \"GET\",\n \"path\": \"/*\"\n }\n ],\n \"redirection\":\"https://www.mywebsite.com/\"\n}");
尝试:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
'accessRules' => array(
0 => array(
'method' => 'GET',
'path' => '/*'
)
),
'redirection' => 'https://www.mywebsite.com/'
)));
但是在curl cli调用中,您发送了标题Content-type: application/json
那么为什么您的php代码发送标头Content-Type: application/x-www-form-urlencoded
?尝试解决这个问题,
$headers[] = "Content-type: application/json";
那么您的代码可以工作吗?