0

我想使用 Watson 的 Personality Insight Service 来分析用户的推文。我正在使用 PHP 进行编码。这是我用来从 Twitter API 获取推文并将它们输入到 watson 的 PI 服务中的代码块。

    //Watson PI Parameters
$pi_url = 'https://gateway.watsonplatform.net/personality-
insights/api/v3/profile?version=2016-10-20';
$pi_username = 'XXXX';
$pi_password = 'YYYY';
// auth parameters
$api_key = urlencode('API KEY'); // Consumer Key 
$api_secret = urlencode('API Secret'); // Consumer Secret
$auth_url = 'https://api.twitter.com/oauth2/token'; 

// Twitter Data Parameters
$data_username = 'Screen Name'; // username
$data_count = 10; // number of tweets
$data_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?
tweet_mode=extended';

// get api access token
$api_credentials = base64_encode($api_key.':'.$api_secret);

$auth_headers = 'Authorization: Basic '.$api_credentials."\r\n".
            'Content-Type: application/x-www-form-urlencoded;charset=UTF-
8'."\r\n";

$auth_context = stream_context_create(
array(
    'http' => array(
        'header' => $auth_headers,
        'method' => 'POST',
        'content'=> http_build_query(array('grant_type' => 
        'client_credentials', )),
      )
    )
  );

 $auth_response = json_decode(file_get_contents($auth_url, 0, 
$auth_context), true);

$auth_token = $auth_response['access_token'];

// get tweets
 $data_context = stream_context_create( array( 'http' => array( 'header' => 
'Authorization: Bearer '.$auth_token."\r\n", ) ) );

 $data = file_get_contents($data_url.'&count='.$data_count.
'&screen_name='.urlencode($data_username), 0, $data_context);
 print_r($data);

 // Pass $data as input to PI
 $ch = curl_init();
 // Sets the URL cURL will open
 curl_setopt($ch, CURLOPT_URL, $pi_url);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
   'Content-Type: application/json',                                                                                
     )                                                                       
  ); 
 // Here's the HTTP auth
   curl_setopt($ch, CURLOPT_USERPWD, $pi_username." : ".$pi_password);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "\CertificateLocation");
 // Makes curl_exec() return server response
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 // And here's the result JSON
  $response = curl_exec($ch);
  print curl_error($ch);
  curl_close($ch);
  print_r($response);

我收到 { "code": 400, "sub_code": "S00005", "error": "Invalid JSON input at line 4, column 28" } 错误。

有人可以帮助我如何预先格式化我的 Twitter 响应,以便 PI 可以将其识别为 Json 输入并对其进行分析。

编辑:

添加推特结果作为答案,因为评论太长了。这是我使用 Amazon twitter 句柄得到的结果(尝试这个只是为了测试)

    [{
    "created_at": "Thu Apr 20 14:49:27 +0000 2017",
    "id": 855070913997492225,
    "id_str": "855070913997492225",
    "full_text": "@realivanspeaks Aww! We love our customers like Buddy loves a his sugar. \ud83d\ude0d \ud83c\udf6c",
    "truncated": false,
    "display_text_range": [
        16,
        76
    ],
    "entities": {
        "hashtags": [
        ],
        "symbols": [
        ],
        "user_mentions": [
            {
                "screen_name": "realivanspeaks",
                "name": "Ivan Speaks",
                "id": 2484007045,
                "id_str": "2484007045",
                "indices": [
                    0,
                    15
                ]
            }
        ],
        "urls": [
        ]
    },
    "source": "\u003ca href=\"https:\/\/www.sprinklr.com\" rel=\"nofollow\"\u003eSprinklr\u003c\/a\u003e",
    "in_reply_to_status_id": 855068504642514944,
    "in_reply_to_status_id_str": "855068504642514944",
    "in_reply_to_user_id": 2484007045,
    "in_reply_to_user_id_str": "2484007045",
    "in_reply_to_screen_name": "realivanspeaks",
    "user": {
        "id": 20793816,
        "id_str": "20793816",
        "name": "Amazon",
        "screen_name": "amazon",
        "location": "Seattle, Washington",
        "description": "Official Twitter of http:\/\/t.co\/4rwjfdidk3. Contact @AmazonHelp for customer support.",
        "url": "http:\/\/t.co\/Z2A4m7UeSv",
        "entities": {
            "url": {
                "urls": [
                    {
                        "url": "http:\/\/t.co\/Z2A4m7UeSv",
                        "expanded_url": "http:\/\/www.amazon.com",
                        "display_url": "amazon.com",
                        "indices": [
                            0,
                            22
                        ]
                    }
                ]
            },
            "description": {
                "urls": [
                    {
                        "url": "http:\/\/t.co\/4rwjfdidk3",
                        "expanded_url": "http:\/\/Amazon.com",
                        "display_url": "Amazon.com",
                        "indices": [
                            20,
                            42
                        ]
                    }
                ]
            }
        },
        "protected": false,
        "followers_count": 2567418,
        "friends_count": 157,
        "listed_count": 13988,
        "created_at": "Fri Feb 13 18:39:54 +0000 2009",
        "favourites_count": 3092,
        "utc_offset": -25200,
        "time_zone": "Pacific Time (US & Canada)",
        "geo_enabled": true,
        "verified": true,
        "statuses_count": 23791,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "is_translation_enabled": false,
        "profile_background_color": "9ACAF8",
        "profile_background_image_url": "http:\/\/pbs.twimg.com\/profile_background_images\/448506280982753280\/GZ6eb7ux.png",
        "profile_background_image_url_https": "https:\/\/pbs.twimg.com\/profile_background_images\/448506280982753280\/GZ6eb7ux.png",
        "profile_background_tile": false,
        "profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/786252488223580160\/9-Lwv1sI_normal.jpg",
        "profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/786252488223580160\/9-Lwv1sI_normal.jpg",
        "profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/20793816\/1492450739",
        "profile_link_color": "FF9900",
        "profile_sidebar_border_color": "FFFFFF",
        "profile_sidebar_fill_color": "FFFFFF",
        "profile_text_color": "000000",
        "profile_use_background_image": false,
        "has_extended_profile": false,
        "default_profile": false,
        "default_profile_image": false,
        "following": null,
        "follow_request_sent": null,
        "notifications": null,
        "translator_type": "none"
    },
    "geo": null,
    "coordinates": null,
    "place": null,
    "contributors": null,
    "is_quote_status": false,
    "retweet_count": 0,
    "favorite_count": 1,
    "favorited": false,
    "retweeted": false,
    "lang": "en"
}]
4

0 回答 0