csensix
V2EX  ›  问与答

PHP 改如何提取这个 openid 值

  •  
  •   csensix · Aug 4, 2014 · 5793 views
    This topic created in 4301 days ago, the information mentioned may be changed or developed.
    如题.
    QQ登录返回的包是这样的:callback( {"client_id":"YOUR_APPID","openid":"YOUR_OPENID"} );

    我想要用其中的openid,有什么好办法可以提取出来么?
    9 replies    2014-08-04 13:58:34 +08:00
    skydiver
        1
    skydiver  
       Aug 4, 2014 via iPad
    substring
    json decode
    csensix
        2
    csensix  
    OP
       Aug 4, 2014
    @skydiver 这是json格式吗?
    wingoo
        3
    wingoo  
       Aug 4, 2014
    先replace,再json_decode
    csensix
        4
    csensix  
    OP
       Aug 4, 2014
    @skydiver 似乎明白了点什么
    MaiCong
        5
    MaiCong  
       Aug 4, 2014
    <?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  
       Aug 4, 2014
    这个其实是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
       Aug 4, 2014
    @feiyuanqiu 赞一个,已经做好功能了。
    zts1993
        8
    zts1993  
       Aug 4, 2014
    jsonp
    icanfork
        9
    icanfork  
       Aug 4, 2014
    jsonp
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5546 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 08:23 · PVG 16:23 · LAX 01:23 · JFK 04:23
    ♥ Do have faith in what you're doing.