J7mbo / twitter-api-php

The simplest PHP Wrapper for Twitter API v1.1 calls
MIT License
1.82k stars 802 forks source link

Issues with SSL certificate #226

Closed SergiTorres closed 7 years ago

SergiTorres commented 7 years ago

I have tried to solve the problem with SSL Certificate on line 315. After looking some other people errors I found you told someone to write on the top ----- var_dump(extension_loaded('curl')); die;

After this the SSL error stops appearing but I have another kind of message:

C:\wamp64\www\test3\twitter-api-php-master\index.php:4:boolean true

This is what appears. I think it should appear a list of my followers. My GET request has been pasted from the readme.md file so i guess it should work.

My entire code:


<?php

var_dump(extension_loaded('curl')); die;
ini_set('display_errors', 1);
require_once('TwitterAPIExchange.php');

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
    'oauth_access_token' => "823454956418199552-JKVTIAwoe2h7XFAh7xhtWyD0fGquibD",
    'oauth_access_token_secret' => "pLXwF3JyYvQwhiM7OOPa0AP3VshmmclmnqOFcvOVBkamM",
    'consumer_key' => "JY3YEo6AJscZcM2bY1A0y2Fsw",
    'consumer_secret' => "be5D5RPQiIc0EDYcAjo8tHq4hGLLSiomGEkydbPvH14AXUhBfk"
);

$url = 'https://api.twitter.com/1.1/followers/ids.json';
$getfield = '?screen_name=J7mbo';
$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
    ->buildOauth($url, $requestMethod)
    ->performRequest();

?>
SergiTorres commented 7 years ago

can you give me some feedback bro?

kaspiCZ commented 7 years ago

Hi,

I'm writing this from memory with a bit of research without actually trying it, so if it doesn't fix it for you, I will go on and dedicate a bit more of my time to reproducing your case.

First a little explanation of what happened:

0) never blindly paste code 1) never paste your actual tokens, secret keys and whatnot 2) var_dump(extension_loaded('curl')); Does output the line you are referring to. It prints out the value of the function call extension_loaded. In your case it is true, so the php-curl extension is loaded 3) die stops the script execution dead preventing all remaining commands from executing. That is why there is no further data.

Now how to fix your problem:

Remove thes line:

var_dump(extension_loaded('curl')); die;

Delete this line AFTER you verify you fixed the problems

(we want to see errors while we test things)

ini_set('display_errors', 1);

Clean solution

Have a look at http://stackoverflow.com/a/16495053 and 1) get a hold of certificate authority bundle (CURL provides their bundle here https://curl.haxx.se/ca/cacert.pem) Please don't take my word for it and look up "curl official site", before you download that file 2) save it (as e.g. cacert.pem) somewhere in your application (the easiest is the same place where the TwitterAPIExchange.php file is) 3) use the CURLOPT_CAINFO option to set up the path to the certificate

$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
    ->buildOauth($url, $requestMethod)
    ->performRequest(true, array('CURLOPT_CAINFO' => "cacert.pem"));

Dirty solution

If you are absolutely sure you can trust the certificates, you can set CURL options to ignore the errors. But before you look at the modified code below, please look up the documentation on php.net for: 1) var_dump, 2) die, 3) extension_loaded Then look up 1) CURLOPT_SSL_VERIFYHOST, 2) CURLOPT_SSL_VERIFYPEER

$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
    ->buildOauth($url, $requestMethod)
    ->performRequest(true, array('CURLOPT_SSL_VERIFYHOST' => 0, 'CURLOPT_SSL_VERIFYPEER' => 0));

Bonus explanation: look at the source code https://github.com/J7mbo/twitter-api-php/blob/master/TwitterAPIExchange.php#L273 where the method you call (performRequest) accepts array of additional options for CURL as a second argument.

SergiTorres commented 7 years ago

Thank you for helping me. I have tried both cases and are not working.

Actually I'm getting this error:

Argument 2 passed to curl_setopt_array() must be of the type array, integer given, called in C:\wamp64\www\test3\twitter-api-php-master\index.php on line 22 and defined in C:\wamp64\www\test3\twitter-api-php-master\TwitterAPIExchange.php on line 308

I'm not giving the properly variables to curl_setopt_array. I've tried a couple of examples I found on internet but still not working.

I'm uploading my source code to github so you are able to reply the case:

https://github.com/SergiTorres/TwitterTEST/tree/master/twitter-api-php-master

Really thanks dude for trying to help me!

kaspiCZ commented 7 years ago

Aha! My bad. I almost had it right. 1) please restore the TwitterAPIExchange.php to original (remove the modification to curl_setopt_array) 2) please have a look at my fork https://github.com/kaspiCZ/TwitterTEST/blob/master/twitter-api-php-master/index.php#L23

What I did incorrectly was passing the constants (CURLOPT_CAINFO, CURLOPT_SSL_VERIFYHOST, CURLOPT_SSL_VERIFYPEER) as strings to the array.

Sorry for the confusion. You can first try with the certificate, or you can comment out the line 23 in my fork if it doesn't work for you and uncomment the line 24

I've tried it out and it worked for me using the cacert.pem, so I'm hoping it will work for you too.

SergiTorres commented 7 years ago

Actually it works! Really thanks for your help dude!

You've been very usefull.

Loves <3!

ahsanmjawaid commented 7 years ago

I am having the same error while retrieving the tweets:

Fatal error: Uncaught exception 'Exception' with message 'SSL certificate problem: unable to get local issuer certificate' in C:\xampp\htdocs\tweets\TwitterAPIExchange.php:315 Stack trace: #0 C:\xampp\htdocs\tweets\index.php(40): TwitterAPIExchange->performRequest() #1 {main} thrown in C:\xampp\htdocs\tweets\TwitterAPIExchange.php on line 315

Can you please guide me as well :( ... I used the same index file which is provided in the project.

kaspiCZ commented 7 years ago

Long story short, it should work for you if you:

  1. download the cacert.pem file from the curl website (curl.haxx.se) and place it in the same directory as your index.php
  2. specify the cacert in your command (see https://github.com/kaspiCZ/TwitterTEST/blob/master/twitter-api-php-master/index.php#L23)

Make sure your apache has read permissions on the cert file.

J7mbo commented 7 years ago

Closing as either this issue has been resolved or it's not actually the library. If you still need help feel free to re-open or add another issue with the current state of your problem :)