2016-09-21 4 views
1

PHPを使用してJSON APIを使用しようとしています。PHPを使用したJSON APIの使用

私は応答を取得するためにCURLを使って試してみました:

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を試しましたか? curlではなく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 

代わりにユーザーを送信するためにCURLOPT_USERPWD/

+1

私は正しい方向に私は次のトークンを受け取ることができたを使用して指してくれてありがとう。 curl -H "Accept:application/json" -H "Content-Type:application/json" - データ '{"電子メール:" myusername "、" password ":" mypassword "}' http:// 104.239.120.178:3000/api/users/authenticate –

0

これを試してみてくださいを渡すの端末でこれを試してみて、あなたがPHPの使用CURLOPT_POSTFIELDSで任意のREPONSE

を持っているかどうかを確認

$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); 
関連する問題