如何通过curl

时间:2017-11-05 22:11:31

标签: php json curl

我一直在尝试用PHP中的Get Request VIA CURL发送JSON数据。 试图将斜线解码编码字符串添加到JSON但失败无法输出我想要的确切字符串, 以下是我希望PHP返回字符串的示例:

http://example.com/android/napp/session.php?session={"session_key":"5.T1lJSZI3WsHvFg.1508724894.24-100021584769980","uid":100021584769980,"secret":"2c3ebc8254d355e3466b17355ca2c6b5","access_token":"EAAAAUaZA8jlABAKbUNcsBEf0HkaCzDnUnbCHQmAQLIFTtCNKlA6SDAZCMNMVp7JV3ZBYjpMfePC0mBconu4L6syQSMXthrOGpKYXZBQ9BzEprfj8wfvAkI3Cy373ZAuCCi7MS78TDbo17XYZBmZCBxjVrYS4pXh9aneODkwfy0Ek5RvA2tPVr0r10myGZC078EwZD","machine_id":"nlDtWb6yQQFtpBnjEmwbdzBZ","session_cookies":[{"name":"c_user","value":"100021584769980","expires":"Tue, 23 Oct 2018 02:14:54 GMT","expires_timestamp":1540260894,"domain":".facebook.com","path":"\/","secure":true},{"name":"xs","value":"24:T1lJSZI3WsHvFg:2:1508724894:13272:7254","expires":"Tue, 23 Oct 2018 02:14:54 GMT","expires_timestamp":1540260894,"domain":".website.com","path":"\/","secure":true,"httponly":true},{"name":"fr","value":"0wOwW7rSJ6acRLZJY.AWUeoVqBbkRqh1YVP6sjD6-ynRM.BZ7VCe..AAA.0.0.BZ7VCe.AWXGSMmw","expires":"Tue, 23 Oct 2018 02:14:54 GMT","expires_timestamp":1540260894,"domain":".website.com","path":"\/","secure":true,"httponly":true},{"name":"datr","value":"nlDtWb6yQQFtpBnjEmwbdzBZ","expires":"Wed, 23 Oct 2019 02:14:54 GMT","expires_timestamp":1571796894,"domain":".website.com","path":"\/","secure":true,"httponly":true}],"identifier":"whatever.pro","user_storage_key":"0f4194ede822da3b281a83fb18e2953df075d97c792d1697024346ca8bca53c3"}

虽然我尝试在curl中使用它,但是它添加了斜杠 正如我这样称呼的那样:

post_data('http://example.com/android/napp/session.php?session={"session_key":"5.T1lJSZI3WsHvFg.1508724894.24-100021584769980","uid":100021584769980,"secret":"2c3ebc8254d355e3466b17355ca2c6b5","access_token":"EAAAAUaZA8jlABAKbUNcsBEf0HkaCzDnUnbCHQmAQLIFTtCNKlA6SDAZCMNMVp7JV3ZBYjpMfePC0mBconu4L6syQSMXthrOGpKYXZBQ9BzEprfj8wfvAkI3Cy373ZAuCCi7MS78TDbo17XYZBmZCBxjVrYS4pXh9aneODkwfy0Ek5RvA2tPVr0r10myGZC078EwZD","machine_id":"nlDtWb6yQQFtpBnjEmwbdzBZ","session_cookies":[{"name":"c_user","value":"100021584769980","expires":"Tue, 23 Oct 2018 02:14:54 GMT","expires_timestamp":1540260894,"domain":".facebook.com","path":"\/","secure":true},{"name":"xs","value":"24:T1lJSZI3WsHvFg:2:1508724894:13272:7254","expires":"Tue, 23 Oct 2018 02:14:54 GMT","expires_timestamp":1540260894,"domain":".website.com","path":"\/","secure":true,"httponly":true},{"name":"fr","value":"0wOwW7rSJ6acRLZJY.AWUeoVqBbkRqh1YVP6sjD6-ynRM.BZ7VCe..AAA.0.0.BZ7VCe.AWXGSMmw","expires":"Tue, 23 Oct 2018 02:14:54 GMT","expires_timestamp":1540260894,"domain":".website.com","path":"\/","secure":true,"httponly":true},{"name":"datr","value":"nlDtWb6yQQFtpBnjEmwbdzBZ","expires":"Wed, 23 Oct 2019 02:14:54 GMT","expires_timestamp":1571796894,"domain":".website.com","path":"\/","secure":true,"httponly":true}],"identifier":"whatever.pro","user_storage_key":"0f4194ede822da3b281a83fb18e2953df075d97c792d1697024346ca8bca53c3"}','');

我尝试使用stripslashes方法但失败了。

我的CURL功能在这里:

function post_data($url, $fields) {

    $cookies = dirname(__FILE__).
    '/cookie.txt';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
    curl_setopt($ch, CURLOPT_TCP_NODELAY, true);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, getRandomUserAgent());
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $body = curl_exec($ch);
    if (!curl_errno($ch)) {
        $info = curl_getinfo($ch);
        $redir = $info['redirect_url'];
        $code = $info['http_code'];
        curl_close($ch);
        return "<br>".$body;


    }
}

1 个答案:

答案 0 :(得分:0)

尝试在json数据上使用urlencode(),所以:

$urlencoded = urlencode('{"session_key":"5.T1lJSZI3WsHvFg.1508724894.24-100021584769980","uid":100021584769980,"secret":"2c3ebc8254d355e3466b17355ca2c6b5","access_token":"EAAAAUaZA8jlABAKbUNcsBEf0HkaCzDnUnbCHQmAQLIFTtCNKlA6SDAZCMNMVp7JV3ZBYjpMfePC0mBconu4L6syQSMXthrOGpKYXZBQ9BzEprfj8wfvAkI3Cy373ZAuCCi7MS78TDbo17XYZBmZCBxjVrYS4pXh9aneODkwfy0Ek5RvA2tPVr0r10myGZC078EwZD","machine_id":"nlDtWb6yQQFtpBnjEmwbdzBZ","session_cookies":[{"name":"c_user","value":"100021584769980","expires":"Tue, 23 Oct 2018 02:14:54 GMT","expires_timestamp":1540260894,"domain":".facebook.com","path":"\/","secure":true},{"name":"xs","value":"24:T1lJSZI3WsHvFg:2:1508724894:13272:7254","expires":"Tue, 23 Oct 2018 02:14:54 GMT","expires_timestamp":1540260894,"domain":".website.com","path":"\/","secure":true,"httponly":true},{"name":"fr","value":"0wOwW7rSJ6acRLZJY.AWUeoVqBbkRqh1YVP6sjD6-ynRM.BZ7VCe..AAA.0.0.BZ7VCe.AWXGSMmw","expires":"Tue, 23 Oct 2018 02:14:54 GMT","expires_timestamp":1540260894,"domain":".website.com","path":"\/","secure":true,"httponly":true},{"name":"datr","value":"nlDtWb6yQQFtpBnjEmwbdzBZ","expires":"Wed, 23 Oct 2019 02:14:54 GMT","expires_timestamp":1571796894,"domain":".website.com","path":"\/","secure":true,"httponly":true}],"identifier":"whatever.pro","user_storage_key":"0f4194ede822da3b281a83fb18e2953df075d97c792d1697024346ca8bca53c3"}');

post_date('http://example.com/android/napp/session.php?session=' . $urlencoded);