将cURL命令翻译成PHP



我有一个cURL命令,我想翻译成PHP。

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"}

也许是因为帖子数据格式不正确?

您必须真实的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);

您也可以使用此网站,https://incarnate.github.io/curl-to-php/它将原始curl命令转换为PHP。

更新:

从ovh文档中,您可以使用用于PHP的ovh api客户端,这是他们文档站点中的示例:

<?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 OvhApi;
/**
* 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 );

首先,这是一种用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";

那么你的代码能工作吗?

最新更新