2016-08-18 6 views
3

私はAPIを使用していますが、POSTリクエストを送信すると、配列内のフィールドの1つが文字列ではなくInteger型でなければならないという警告が表示されます。PHP CURLリクエストで整数を保持する方法

マイCURLのセットアップは、このようなものです:私はこれを取得、私は私のローカルホスト上の別のURLにこれを送信すると

$post_fields = array(
      'data_source_uuid' => $uuid, 
      'name' => 'TestPlan', 
      'interval_count' => 1, 
      'interval_unit' => 'month', 
      'external_id' => 'Eur_fees' 
     ); 
    $curl = curl_init(); 
    curl_setopt_array($curl, array(
     CURLOPT_RETURNTRANSFER => true, 
     CURLOPT_URL => $url, 
     CURLOPT_USERPWD => $api_key 
     CURLOPT_POSTFIELDS => $post_fields, 
     CURLOPT_HTTPHEADER => 'Content-Type: application/json' 
    )); 
    $result = curl_exec($curl); 
    curl_close($curl); 

し、それをのvar_dump:

string(253) "array(5) { 
      ["data_source_uuid"]=> 
      string(39) "uuid" 
      ["name"]=> 
      string(8) "TestPlan" 
      ["interval_count"]=> 
      string(1) "1" 
      ["interval_unit"]=> 
      string(5) "month" 
      ["external_id"]=> 
      string(8) "Eur_fees" 
     }" 

ここでの問題はinterval_countがStringであります整数ではありません。私はCURLOPT_POSTFIELDSを使用する前に私はvar_dumpそれは整数ですので、CURL部分の何かがそれを変更していますが、私は何がわかりません。ドキュメントはここに言うように

APIは(https://dev.chartmogul.com/docs/import-plan)chartmogul.com

+0

https://secure.php.net/language.types.type-juggling#language.types.typecasting – Bolli

+0

、それは保存されていません –

答えて

1

ここからChartMogulの請求書。 json_encode($data)でデータをエンコードする必要があります。また、データソースのUUID、アカウントの秘密鍵、アカウントトークンが正しいことを確認してください。次のリクエストは、私の作品:

それはCURLOPT_POSTFIELDS前のIntであるように動作しない
<?php 

// account variables 
$ds_uuid = "DATA_SOURCE_UUID"; 
$token = 'API_TOKEN'; 
$password = 'SECRET_KEY'; 

// request url 
$baseurl='https://api.chartmogul.com/v1/'; 
$url=$baseurl.'import/plans'; 

// data to be posted 
$post_fields = array(
      'data_source_uuid' => "$ds_uuid", 
      'name' => 'A plan', 
      'interval_count' => 1, 
      'interval_unit' => 'month', 
      'external_id' => 'eur_fees' 
     ); 

// encode json data 
$data = json_encode($post_fields); 

// initialize cURL 
$ch = curl_init(); 

// set options 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, "$token:$password"); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json', 
    'Content-Length: ' . strlen($data)) 
); 

// make the request 
$result = curl_exec($ch); 

// decode the result 
$json = json_decode($result, true); 

// print the result 
print $json; 

curl_close($ch); 
?>`enter code here` 
+0

さて、ChartMogulにはPHPクライアントライブラリがあります:https://github.com/chartmogul/chartmogul-php。 – B1ll

1

と呼ばれるウェブサイトで、あなたはJSONデータを送信する必要があります。代わりにCURLOPT_POSTFIELDS => $post_fields

、あなたがCURLOPT_POSTFIELDS => json_encode($post_fields)

EDITを使用する必要があります。また、ドキュメントは、あなたが、一つは「data_source_uuid」という名前の必要なデータのChartMogul UUIDを含む文字列を忘れてしまったあなたは5つのパラメータを送信するために持っていることを述べていますこのサブスクリプションプランのソース。

+0

それは動作しません。私がするとき、メッセージは "エラー"です: "プロパティ '#/'は 'data_source_uuid'の必須のプロパティを含んでいませんでした。私はCURLOPT_POSTFIELDSが自動urlencodingを行うためだと思います –

+0

"data_source_uuid"パラメータ(このサブスクリプションプランのデータソースのChartMogul UUIDが含まれています)を送信するのを忘れたので、はい、そうです。 – Julqas

+0

Nope。私はそれを私の例から除外しましたが、私があなたの提案の前に配列に入れたとき、それはうまくいきません。私は今それを含めるために私の質問を更新します。 –

関連する問題