首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用PHP使用JSON

使用PHP使用JSON
EN

Stack Overflow用户
提问于 2016-09-21 06:01:31
回答 2查看 1.6K关注 0票数 1

我正在尝试使用PHP来使用JSON。

我试着用卷发得到回应:

代码语言:javascript
运行
复制
curl 'http://104.239.130.176:3000/api/users/authenticate?email=my_username_here&password=my_password'

这在终点站没有给我任何回应。

我也试过用电子邮件换用户名:

代码语言:javascript
运行
复制
curl 'http://104.239.130.176:3000/api/users/authenticate?username=my_username_here&password=my_password'

我在PHP文件中编写了以下内容,但这给了我浏览器中的404错误。

代码语言:javascript
运行
复制
<?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安全性,所以第一个请求令牌。

将请求发送到:http://104.239.176.130:3000/api/users/authenticate

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-21 06:22:34

代码语言:javascript
运行
复制
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发送用户/pass

票数 1
EN

Stack Overflow用户

发布于 2016-09-21 06:35:33

尝尝这个

代码语言:javascript
运行
复制
 $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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39608589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档