douxuanyi2813 2018-08-21 15:35
浏览 39
已采纳

从卷曲中解析字符串

<?php 

$curl = curl_init();

    curl_setopt_array($curl, array(
  CURLOPT_URL => "",
  //return the transfer as a string
  CURLOPT_RETURNTRANSFER => true,
  //enable headers
  CURLOPT_HEADER => true,
  //get only headers
  CURLOPT_NOBODY => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "Accept: application/json",
    "password: ",
    "username: "
  ),
));
// $response contains the output string
$response = curl_exec($curl);

$err = curl_error($curl);

curl_close($curl);

print_r($response);

?>

I am trying to take $response and truncate data from it.My output of $response is

HTTP/1.1 200 OK Server: Apache-Coyote/1.1 access-token: a3581f021476e4fb406c6b7c79e4095cece5d0c6 token-expiration-hours: 24 Content-Type: application/json Content-Length: 0 Date: Tue, 21 Aug 2018 14:12:27 GMT

I would like $response to just be the value of the access-token a3581f021476e4fb406c6b7c79e4095cece5d0c6

  • 写回答

4条回答 默认 最新

  • duanmei2459 2018-08-21 15:41
    关注

    One option is to use regex to find the access token:

    preg_match('/access-token: ([^\s]+) /', $response, $matches);
    vaR_dump($matches[1]); //a3581f021476e4fb406c6b7c79e4095cece5d0c6
    

    This will match any string after the string access-token, up to any whitespace. Just make sure the access-token is not the last item in the headers as there probably won't be any whitespace...

    Assumption: the access token will never contain any white space.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程