2016-09-21 2 views
1

PHP로 JSON API를 사용하려고합니다. 나는 응답을 얻기 위해 CURL을 사용하여 시도했다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을 시도 했습니까? 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 

터미널에서 이것을 시도하고 당신이 PHP를 사용 CURLOPT_POSTFIELDS에서 어떤 reponse

가 있는지 확인하는 대신 사용자를 보낼 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

이 시도 통과

$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);