I decided to combine my website with my twitter account. In order to use it properly, I wanted to post a tweet. I decided to go with the very simple class TwitterAPIExchange from here: https://github.com/J7mbo/twitter-api-php
I tested it on my Linux server and it all works well. But I wanted to use this tool from my Windows server. This is my set up: Windows 2012, IIS 8, PHP.
Here my test code
// Setup
$settings = array( ... );
$url = 'https://api.twitter.com/1.1/statuses/update.json';
// Post the data
$requestMethod = 'POST';
$postfields = array('status' => 'Hello World');
$twitter = new TwitterAPIExchange($settings);
$json = $twitter->buildOauth($url, $requestMethod)->setPostfields($postfields)->performRequest();
var_dump($json);
On the windows machine, it returns
bool(false)
all the time! What does that mean? Is it an issue with curl and IIS? I read several articles online about it but none helped me so far. Any input is very appreciated!
Curl gave me the following error:
curl iis SSL certificate problem, verify that the CA cert is OK
As I could not make it work with a certificate on IIS, I folllowed a hint given on a forum (http://board.phpbuilder.com/showthread.php?10253357-cURL-and-SSL-issues) which told me to add the following curl options:
curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER , false );
curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST , false );
So in the end, I added this to the TwitterAPIExchange class where curl was used.
You likely have to download a certificate package and point the script to it. The following should work:
Save this file to the same directory as the PHP files: http://curl.haxx.se/ca/cacert.pem
Add CURLOPT_CAINFO => 'cacert.pem' to the options section, which starts at line 192 of the current version of the code: https://github.com/J7mbo/twitter-api-php/blob/master/TwitterAPIExchange.php#L192
You could also use another valid certificate bundle and save it elsewhere, so long as you include the path to it in the code.
Related
I have PHP Laravel5 project. I use file_get_contents() in a controller's action like the following:
$production = json_decode(file_get_contents(url('/operation/get-production/'.$job->id)))->production;
The above route is excluded from authentication and authorization and the above line of code works well from the localhost. However, when a remote user from the Internet uses that code, it generates the following error:
I'm not using IIS, the server is Apache on Ubuntu linux. I don't know why SSL is appeared in this case? I'm pretty sure that the URL supplied to the function is http://... not https://...
Your production URL may be forced to use https (and it's a good thing).
file_get_contents() isn't the perfect choice to get data over https but if php_openssl extension is activated and allow_url_fopen to set to "on" you can get content from https.
As you seems to don't have a valid SSL certificate : use PHP Curl must be a better idea ( http://php.net/manual/fr/book.curl.php ), as you can disabled SSL error with this :
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
(if you trust the URL : you could do this, never do this with an external website)
It sounds like the certificate verification is failing because it doesn't have a reference file of root certificates to check it with. See this Stack Overflow answer.
You have 2 possible situations:
Possible Answer #1, if you are using apache, check your conf and see if your domain is not forcing all calls to use SSL
OR -
Possible Answer #2 you need to create a flow for your GET url and send headers to the other server:
<?php //Create a flow
$opt = array(
'http'=>array(
'method'=>'GET',
'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3',
'header'=>'Accept-language: en\r\n' .
'Cookie: foo=bar\r\n’ ) );
$context = stream_context_create($opt);
//Get external url response
$file = file_get_contents('http://www.example.com/some_file', false, $context); ?>
I'm connecting to a remote server using TLS1.1 on PHP 5.3.
When using Zend Framework 2, I get an error:
$client = new Client('https://www.example.com/');
$curlAdapter = new Client\Adapter\Curl();
$curlAdapter->setCurlOption(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_1);
$client->setAdapter($curlAdapter);
$client->send();
Result: Error in cURL request: SSL connect error
Adding this resolves the issue, but is obviously less secure
$curlAdapter->setCurlOption(CURLOPT_SSL_VERIFYHOST, 2);
$curlAdapter->setCurlOption(CURLOPT_SSL_VERIFYPEER,false);
Result: It works
Making the request using native PHP commands works fine:
$c = curl_init('https://www.example.com/');
$options = array(
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_1,
);
curl_setopt_array ($c ,$options );
curl_exec($c);
Returns the contents of the page.
So PHP works, but ZF2 doesn't unless VerifyPeer = false. What's the issue?
It is probably because you are missing one parameter:
CURLOPT_CAINFO => '/etc/ssl/certs/ca-bundle.pem' // replace with your cert.
It is also possible that you are using different php configurations (web / cli) that point to different places with the ssl certs. Some details are also available here: Security consequences of disabling CURLOPT_SSL_VERIFYPEER (libcurl/openssl)
I have a situation while trying to do a GET method with curl to get xml from distant web api.
$url = 'xxxx.com'
$key = '/KEY/PRIVATE.KEY'
$perm = '/KEY/CERT.PEM'
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSLCERT => $perm ,
CURLOPT_SSLKEY => $key,
CURLOPT_HEADER => false,
);
$curl = curl_init();
curl_setopt_array($curl , $options);
$resp = curl_exec($curl);
curl_close($curl);
I have to link SSL cert and pem, but $resp is always returned as false.
This is what my cURL request should be in command prompt :
curl -k -v -X GET "[-URL-]" --cert ./KEY/CERT.pem --key ./KEY/PRIVATE.KEY
Any help would be strongly appreciated...
EDIT
So, I used two methods to try to debug :
With stderr I have:
string ''.' unknown as intern or extern command, program file or executable file
' (length=117)
And with die(curl_error($curl)) I get this :
unable to use client certificate (no key found or wrong pass phrase?)
So, I actually make my URL with two string ($url = a . b), but this is supposed to work, or am I crazy?
For the ssl error, I don't get why I have this issue since this works totally fine on command prompt...
EDIT 2
So, I found out how to solve in part my issue : I had to use realpath().
But now I have a new issue :
SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
So, I found some responses on stackoverflow that say to update cacert.pem file.
I did it, added CURLOPT_CAINFO => $cainfo,
in $options and put curl.cainfo = "PATH_TO/cacert.pem" in both of my php.ini (apache + php => WAMP). (PATH_TO already replaced by my true path).
Anyway, after all thoses changes, I still have the same issue.
I have to work under php 5.3, would it be the cause?
EDIT 3
Finnally found my answer. I should have look for curl options more carefully...
In command prompt -k allows an insecure connexion.
So, in php, I just have to set CURLOPT_VERIFYPEER to false.
FYI, I based myself on the doc that the distant web api gave to me .
Regards,
I had a lot of trouble figuring out why my php Curl API worked fine on a Mac using MAMP, but would not work under windows.
I asked fot debugging tips or useful information for finding curl configuration issues under windows.
The accepted answer contains a list of the steps that helped me get curl working on windows 7 32 bits.
If Curl still doesn't work, you can use file_get_contents to make POST requests. It works on all hostingers, all OS and on local.
$url = 'WhateverUrlYouWant';
$postdata = http_build_query(
array(
'id' => '202',
'form' => 'animal',
.....
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url,false, $context);
echo $result
Here is a list of steps for debugging Curl:
Check in phpinfo module that Curl IS enabled.
Verify extensions dir path is propperly set in php.ini and that extension=php_curl.dll is uncommented.
Check that Environment Variables are propperly set as per: http://php.net/manual/en/faq.installation.php#faq.installation.addtopath
Run deplister.exe ext\php_curl.dll to verify all dependencies are correctly satisfied.
If all of the above is working then check the output of CURLOPT_VERBOSE. As per #hp95 suggestion in the following thread: No Response getting from Curl Request to https server
If you are reaching a site that uses SSL check the following post: HTTPS and SSL3_GET_SERVER_CERTIFICATE:certificate verify failed, CA is OK
And fix it like these:
https://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/
reverse
curl_close($curl);
print_r($response);
** this is a broken protocol **
to
print_r($response);
curl_close($curl);
** print your results BEFORE closing, which destroys (empties) the $response **
I have just downloaded the latest xampp and it is my first time to use curl.
I have an account in appannie and have read one of the posts here regarding his/her attempt to access appannie.
Here is the link of that post:
Appannie api basic authentication
Well, to make it simpler here is that code he made:
<?php
$whmusername = "username";
$whmpassword = "password";
$query = "https://api.appannie.com/v1/accounts";
$ch = curl_init();
// Sets the URL cURL will open
curl_setopt($ch, CURLOPT_URL, $query);
// Here's the HTTP auth
// The 3rd argument is your Twitter username and password joined with a colon
curl_setopt($ch, CURLOPT_USERPWD, $whmusername.":".$whmpassword);
// Makes curl_exec() return server response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// And here's the result XML
$response = curl_exec($ch);
curl_close($ch);
// inserted: echo 'hello';
print $response;
// inserted: echo 'world';
?>
I tried to use that but nothing happened. So i inserted echo (which i have labeled in the comment) both before and after response to see if my php network is really working and it just displayed:
hello world
So i was wondering if curl library in xampp and have read this post:
curl not working in xampp localhost
In the comments were the steps but what i found out was the curl in php.ini in php dir is already not in comment form and there was no php.ini in apache dir.
And i the 1st post that i referred regarding appannie, i have already change the username into the email account used in appannie.
Please any help? Thanks
There are three different php.ini files that require modification for the cURL library to work on XAMPP.
C:\Program Files\xampp\apache\bin\php.ini
C:\Program Files\xampp\php\php.ini
C:\Program Files\xampp\php\php4\php.ini
You need to uncomment (if not already) the line for cURL Extension:
;extension=php_curl.dll
^----- remove semi-colon
Once you've made the change, you'll need to restart XAMPP.
Also, make sure that there are two files in your Windows system32 folder: libeay32.dll and ssleay32.dll. If these files are missing, you can copy them over from the PHP folder.
Hope this helps!