Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 条带API创建SKU_Php_Curl_Hash_Stripe Payments_Sku - Fatal编程技术网

Php 条带API创建SKU

Php 条带API创建SKU,php,curl,hash,stripe-payments,sku,Php,Curl,Hash,Stripe Payments,Sku,我需要通过条带API创建SKU 问题在于库存领域。 条带api响应为: 'error' => [ 'message' => 'Invalid hash', 'param' => 'inventory', 'type' => 'invalid_request_error' ] 我的php代码是: $endPoint = 'https://api.stripe.com/v1/skus'; $APIKEY_TEST = 'my_api_

我需要通过条带API创建SKU

问题在于库存领域。 条带api响应为:

'error' => [
    'message' => 'Invalid hash',
    'param' => 'inventory',
    'type' => 'invalid_request_error'
]
我的php代码是:

    $endPoint = 'https://api.stripe.com/v1/skus';
    $APIKEY_TEST = 'my_api_key';
    $headers = array('Authorization: Bearer '.$APIKEY_TEST);

    $sku = [
        'active' => 'true',
        'inventory' => ['quantity' => 10000000 ,'type' => 'infinite', 'value' => null],
        "currency" => "eur",
        "price" => $price,
        "product" => $stripe_product_id
    ];

    $array_string ='';
    foreach($sku as $key => $value) {
        $array_string .= $key.'='.$value.'&';
    }
    rtrim($array_string, '&');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $endPoint);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $array_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $output = curl_exec($ch);
    curl_close($ch);
在条带api文档中,库存是哈希类型字段。 我尝试了json_encode(),但运气不好。 可能问题在于发送数组而不是散列。 在$sku数组中,库存字段也是嵌套的关联数组。 也许问题也存在于此

是否有一种方法可以发送包含库存的CURLOPT_POSTFIELDS,以便stripe接受它

编辑: 在Stripe dashboard中,我可以看到我的请求:

  {
  "active": "true",
  "inventory": "Array",
  "currency": "eur",
  "price": "3",
  "product": "prod_F6ipvfYFvOxxQq"
}

Inventory字段没有数据,而是“Array”。

在尝试了许多可能的解决方案后,我找到了答案:

$post_array = http_build_query($sku);
并且知道stripe接受带有嵌套库存数组的$sku数组

值得注意的是,stripe在请求中不接受JSON


请求必须进行url编码。

您尝试过吗?散列值应该像
inventory[type]=finite
等那样进行编码,但是您的循环并没有遍历更深的数组维度。我认为
$array\u string=http\u build\u query($sku)
可能会为您提供所需的格式。我刚刚回来给出答案,因为我在几分钟前找到了这个php函数,它解决了问题。非常感谢。