V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
csensix
V2EX  ›  问与答

PHP 改如何提取这个 openid 值

  •  
  •   csensix · 2014-08-04 11:54:58 +08:00 · 5197 次点击
    这是一个创建于 3555 天前的主题,其中的信息可能已经有所发展或是发生改变。
    如题.
    QQ登录返回的包是这样的:callback( {"client_id":"YOUR_APPID","openid":"YOUR_OPENID"} );

    我想要用其中的openid,有什么好办法可以提取出来么?
    9 条回复    2014-08-04 13:58:34 +08:00
    skydiver
        1
    skydiver  
       2014-08-04 11:56:56 +08:00 via iPad
    substring
    json decode
    csensix
        2
    csensix  
    OP
       2014-08-04 11:57:37 +08:00
    @skydiver 这是json格式吗?
    wingoo
        3
    wingoo  
       2014-08-04 11:57:44 +08:00
    先replace,再json_decode
    csensix
        4
    csensix  
    OP
       2014-08-04 11:58:02 +08:00
    @skydiver 似乎明白了点什么
    MaiCong
        5
    MaiCong  
       2014-08-04 12:13:01 +08:00
    <?php
    $str = 'callback({"client_id":"YOUR_APPID","openid":"YOUR_OPENID"});';
    $lpos = strpos($str, "(");
    $rpos = strrpos($str, ")");
    $str = substr($str, $lpos+1, $rpos-$lpos-1);
    $json = json_decode($str);
    // print_r($json);
    $openid = $json->openid;
    echo $openid;
    feiyuanqiu
        6
    feiyuanqiu  
       2014-08-04 12:33:22 +08:00
    这个其实是qq准备让你用js来运行的...

    ```php
    $callback = 'callback( {"client_id":"YOUR_APPID","openid":"YOUR_OPENID"} )';
    if (preg_match('/\"openid\":\"(\w+)\"/i', $callback, $match)) {
    $openid = $match[1];
    }
    var_dump($openid);exit;
    ```
    略菜...勿喷
    csensix
        7
    csensix  
    OP
       2014-08-04 12:47:31 +08:00
    @feiyuanqiu 赞一个,已经做好功能了。
    zts1993
        8
    zts1993  
       2014-08-04 13:19:32 +08:00
    jsonp
    belin520
        9
    belin520  
       2014-08-04 13:58:34 +08:00
    jsonp
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3529 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 04:53 · PVG 12:53 · LAX 21:53 · JFK 00:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.