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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?