2016-09-21 73 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);