2016-06-23 26 views
1

我讀了一些Q &但仍然在爲此付出努力。我需要發佈特定的數組到一個API並獲得另一個數組作爲答案。無法使用PHP製作CURL發佈請求

UPDATE

我用:

<?php 

echo 'Testing cURL<br>'; 

// Get cURL resource 
    $curl = curl_init(); 
    $data=array(array("UserId"=>"xxxx-10100","Password"=>"pass")); 
    $sendpostdata = json_encode(array("postdata"=> $data)); 
    echo $sendpostdata; 
// Set some options - we are passing in a useragent too here 
    curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1, 
    CURLOPT_URL => 'https://cloud.servicebridge.com/api/v1/Login', 
    CURLOPT_HTTPHEADER => array('Accept: application/json','Content-Type: application/json'), 
    CURLOPT_USERAGENT => 'Codular Sample cURL Request', 
    CURLOPT_POST => 1, 
    CURLOPT_POSTFIELDS => $sendpostdata 
)); 
// Send the request & save response to $resp 
    $resp = curl_exec($curl); 
    echo $resp; 
// Close request to clear up some resources 
    curl_close($curl); 

?> 

這導致

Testing cURL {"postdata":[{"UserId":"xxxxx-10100","Password":"pass"}]} { "Data": null, "Success": false, "Error": { "Message": "Invalid UserId: ", "Value": "InvalidUserId", "Code": 9001 } }

沒有控制檯日誌,沒有其他消息或線索 我想實現這個API https://cloud.servicebridge.com/developer/index#/ 到Worpdress。

的API需要

{ "UserId": "string", "Password": "string" }

你能幫助我嗎?我在做什麼錯

非常欣賞這一點,

Giannis

+0

感謝你的建議。它沒有產生任何錯誤,但它對未來有用的知識 –

+0

的事情是,當我在API測試網站https://cloud.servicebridge.com/developer/index#/中使用完全相同的字符串時,它們工作。我只是複製粘貼。還有什麼其他的東西 –

+0

以及響應體中的''Data「:null」'是什麼意思?你認爲它是相關的嗎? –

回答

1

我已簽了給API和它的REQUEST Parameters to access them,我認爲你的初始數據(username and password)不會正確。請使用此代碼: -

<?php 
error_reporting(E_ALL); // check all type of errors 
ini_set('display_errors',1); // display those errors 
// Get cURL resource 
$curl = curl_init(); 
$sendpostdata = json_encode(array("UserId"=>"xxxx-10100","Password"=>"pass")); 
// Set some options - we are passing in a useragent too here 
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1, 
    CURLOPT_URL => 'https://cloud.servicebridge.com/api/v1/Login', 
    CURLOPT_HTTPHEADER => array('Accept: application/json','Content-Type: application/json'), 
    CURLOPT_USERAGENT => 'Codular Sample cURL Request', 
    CURLOPT_POST => 1, 
    CURLOPT_POSTFIELDS => $sendpostdata 
)); 
// Send the request & save response to $resp 
$resp = curl_exec($curl); 
echo $resp; 
// Close request to clear up some resources 
curl_close($curl); 
?> 

注: - 改變UserId & Password值,你的真實values.Thanks

+0

@你好,歡迎你 –

0

這可以幫助你在正確生成JSON。希望這會起作用。

使用本

$data=array(array("UserId"=>"xxxxx-10100","Password"=>"pass")); 

,而不是

$data ='[{"UserId":"xxxxx-10100","Password": "pass"}]'; 
+0

請檢查更新,非常感謝你的建議。現在提交POST。 –