2016-09-21 70 views
1

我正在嘗試使用PHP使用JSON API。使用PHP使用JSON API

我一直在使用捲曲得到迴應嘗試:

curl 'http://104.239.130.176:3000/api/users/authenticate?email=my_username_here&password=my_password' 

這不會給我在終端任何迴應。

我也曾嘗試爲用戶名交換電子郵件:

curl 'http://104.239.130.176:3000/api/users/authenticate?username=my_username_here&password=my_password' 

我已經寫在一個PHP文件中的下列但是這給了我在瀏覽器中一個404錯誤。

<?php 
    // Parameters 
    $tpm_base_url = 'http://104.239.176.130:3000/'; // ending with/
    $req_uri = 'api/users/authenticate'; // GET /passwords.json 
    $username = 'my_username'; 
    $password = 'my_password'; 

    // Request headers 
    $headers = array( 
    'Content-Type: application/json; charset=utf-8' 
); 

    // Request 
    $ch = curl_init($tpm_base_url . $req_uri); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_HEADER, TRUE); // Includes the header in the output 
    curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password); // HTTP Basic Authentication 
    $result = curl_exec($ch); 
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close($ch); 

    // Get headers and body 
    list($headers, $body) = explode("\r\n\r\n", $result, 2); 
    $arr_headers = explode("\r\n", $headers); 
    $arr_body = json_decode($body, TRUE); 

    // Show status and array of passwords 
    echo 'Status: ' . $status . '<br/>'; 
    print_r($arr_body); 

我不知道我在哪裏錯了。來自API開發者的指令如下:

該API具有基本的JWT安全性,因此第一個請求令牌。

POST請求:在終端 http://104.239.176.130:3000/api/users/authenticate

+0

你試過file_ge t_contents(url)而不是捲曲? –

+0

** POST **請求....據我所知,你只是在做GET請求。你應該使用一個適當的HTTP庫,而不是試圖推出自己的。會爲你節省很多時間。 –

回答

1
curl -H "Accept: application/json" -H "Content-Type: application/json" --data "username=my_username_here&password=my_password" http://104.239.176.130:3000/api/users/authenticate 

試試這個,看看如果您有任何效應初探

在PHP中使用CURLOPT_POSTFIELDS代替CURLOPT_USERPWD發送用戶名/密碼

+1

感謝您使用以下方式將我指向正確的方向我能夠收回令牌。 curl -H「Accept:application/json」-H「Content-Type:application/json」--data'{「email」:「myusername」,「password」:「mypassword」}'http:// 104.239.120.178:3000/api/users/authenticate –

0

試試這個

$ch = curl_init($tpm_base_url . $req_uri); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_HEADER, TRUE); // Includes the header in the output 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, 
      "email=$username&password=$password"); 
$result = curl_exec($ch); 
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close($ch);