2016-03-30 64 views
0

我是新的PHP捲曲......我只是想知道如何把這個curl命令到PHP:捲曲與陣列PHP,也許簡單的答案

curl https://ancient-test.chargebee.com/api/v1/portal_sessions \ 
    -u test_rdsfgfgfddsffds: \ 
    -d customer[id]="EXAMPLE" \ 
    -d redirect_url="https://yourdomain.com/users/3490343" 

現在我已經得到了:

$post_data['customer']['id'] = "EXAMPLE"; 
$post_data['redirect_url']  = "http://" . SITE_URL . "/myaccount/"; 
$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL,"https://ancient-test.chargebee.com/api/v1/portal_sessions"); 
curl_setopt($ch,CURLOPT_USERPWD,"test_rdsfgfgfddsffds:"); 
curl_setopt($ch,CURLOPT_POST,1); 
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data); 
$output = curl_exec($ch); 
curl_close($ch); 

但我得到的錯誤信息:

{ 「錯誤」:[{ 「消息」: 「有在提交錯誤」},{ 「參數」:「顧客[ID ]「,」消息「:」不能是bla nk「}]}

感謝您的幫助!

+0

是'$ customer ['id']'empty? – Kisaragi

+0

您是否在項目中使用了Doctrine? – Tobias

+0

php的curl擴展不處理多維數組(afaik)。您應該使用'curl_setopt($ ch,CURLOPT_POSTFIELDS,http_build_query($ post_data));'或手動創建第一維數組鍵。但是,您當前的代碼應該生成一個數組到字符串的轉換警告。你確定顯示的代碼是你測試的代碼嗎? – Rangad

回答

-1

使用curl這裏也許答案Posting with PHP and Curl, deep array

$post_data['customer[id]'] = "EXAMPLE"; 

Guzzle是在PHP中真棒HTTP客戶端庫包裝捲曲,這將使你的生活比較容易的方式:)

用guzzle v6你的php代碼看起來像這樣:

$client = new GuzzleHttp\Client(); 
$res = $client->request('POST', 'https://ancient-test.chargebee.com/api/v1/portal_sessions', [ 
    'auth' => ['test_rdsfgfgfddsffds', 'password'], 
    'json' => [customer => [id => 'EXAMPLE']] 
]); 
+0

會喜歡使用Guzzle,但它需要作曲家...我沒有共享主機環境。 –

+0

我已經更新了我的答案,你試過了嗎? – olaurendeau