繁体   English   中英

将cURL命令翻译成PHP

[英]Translate cURL command into 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 doc中,您可以将OVH api客户端用于PHP,这是他们在其doc网站中的示例:

<?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 );

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

那你的代码行吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM