csensix
V2EX  ›  问与答

PHP 改如何提取这个 openid 值

  •  
  •   csensix · Aug 4, 2014 · 6214 views
    This topic created in 4488 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
    skydiver
    Aug 4, 2014 via iPad
    1
    substring
    json decode
    csensix
    csensix
    OP
    Aug 4, 2014
    2
    @skydiver 这是json格式吗?
    wingoo
    wingoo
    Aug 4, 2014
    3
    先replace,再json_decode
    csensix
    csensix
    OP
    Aug 4, 2014
    4
    @skydiver 似乎明白了点什么
    MaiCong
    MaiCong
    Aug 4, 2014
    5
    <?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
    feiyuanqiu
    Aug 4, 2014
    6
    这个其实是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
    csensix
    OP
    Aug 4, 2014
    7
    @feiyuanqiu 赞一个,已经做好功能了。
    zts1993
    zts1993
    Aug 4, 2014
    8
    jsonp
    icanfork
    icanfork
    Aug 4, 2014
    9
    jsonp
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1088 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 18:36 · PVG 02:36 · LAX 10:36 · JFK 13:36
    ♥ Do have faith in what you're doing.