I want to rewrite the file_get_contents function of php with the curl function of PHP.

Asked 2 years ago, Updated 2 years ago, 145 views

I rewrite the code written using the file_get_contents function with the curl function of PHP.

Warning:Illegal string offset 'access_token'

The message appears, and you cannot retrieve the tweet on Twitter.If you know how to solve this problem, please reply.

This is a link for the entire code.
https://github.com/sizaki30/TwitterAppOAuth/blob/master/TwitterAppOAuth.php

I am creating a site to get tweets by referring to this person's site.
Get over 100 tweets on Twitter API search/tweets (PHP)

This is a site that I used as a reference for curl.
Migrate from file_get_contents to curl in php -Qiita
Reasons and alternatives to using file_get_contents() for API etc. -Qiita

private function_getBearerToken($consumer_key,$consumer_secret){
    $oauth2_url='https://api.twitter.com/oauth2/token';
    $token=base64_encode(urlencode($consumer_key).':'.urlencode($consumer_secret));

    $request=array(
        'grant_type' = > 'client_credentials'
    );
    $header=[
        'Content-type: application/x-www-form-urlencoded; charset=UTF-8',
        'Authorization: Basic'.$token,
    ];

    $curl=curl_init();
    curl_setopt($curl,CURLOPT_URL,$oauth2_url); 
    curl_setopt($curl,CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($curl,CURLOPT_POST,true);
    curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($request)));
    curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl,CURLOPT_HEADER,true);

    $response_arr=curl_exec($curl);
    curl_close($curl);
    return $response_arr ['access_token']; // where Warning: appears
    }

    public function get($api,$params=array())
    {
        $api_url='https://api.twitter.com/1.1/'.$api.'.json';

        if($params){
            $request=http_build_query($params,', '&', PHP_QUERY_RFC3986);
            $api_url.='?'.$request;
        }

        $header=[
          'Authorization: Bearer'.$this->_bearer_token,
        ];
        $curl=curl_init();
        curl_setopt($curl,CURLOPT_URL,$api_url);
        curl_setopt($curl,CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($curl,CURLOPT_POST,true);
        curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($request)));
        curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl,CURLOPT_HEADER,true);

        $response_json=curl_exec($curl);
        curl_close($curl);
        return$response_json;
    }

php php-curl

2022-09-30 16:48

1 Answers

How about like this?
We recommend that you set CURLOPT_SSL_VERIFYPEER to false because SSL authentication may fail.

$request=array(
        'grant_type' = > 'client_credentials'
    );
$header=[
    'Content-type: application/x-www-form-urlencoded; charset=UTF-8',
    'Authorization: Basic'.$token,
];


$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,$oauth2_url);
curl_setopt($curl,CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl,CURLOPT_POST,true);
curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($request)));
curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,CURLOPT_HEADER,true);

$response_json=curl_exec($curl);
curl_close($curl);


2022-09-30 16:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.