I have a PHP script that tries to connect with an other PHP script located on a IIS installation running on a local machine in my office using the cURL protocol but fails to do so.
Local configuration:
I've opened the port 8789 on the firewall, I made the router redirect correctly to the IP of the machine running IIS and I've set the default port of IIS to 8789.
If I try to connect to the script (93.144.xxx.xxx:8987/curl_response.php) through a browser I get the resulting output so this setup is working correctly.
When I try to run the PHP script on a remote webserver though it gives my either cUrl error (#7): couldn't connect to host or cUrl error (#28): connect() timed out! (I'm trying different webservers)
The curl part of my PHP script is the following:
$url = 'http://93.144.xxx.xxx:8789/curl_response.php';
$verbose = fopen('php://temp', 'w+');
$curl = curl_init();
curl_setopt ($curl, CURLOPT_VERBOSE, true);
curl_setopt ($curl, CURLOPT_STDERR, $verbose);
curl_setopt ($curl, CURLOPT_URL, $rurl);
curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt ($curl, CURLOPT_HEADER, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, false);
curl_setopt ($curl, CURLOPT_PORT , 8789);
$response = curl_exec($curl);
if ($response === FALSE) {
printf("cUrl error (#%d): %s<br>\n", curl_errno($curl),
htmlspecialchars(curl_error($curl)));
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog),"</pre>\n";
}else{
curl_close($curl);
// trim response (serialized array)
$response = trim($response);
// unserialize
$response = unserialize($response);
print_r($response);
}
I've tried several different CURLOPT_ configurations and I've even tried using a dyndns host instead of the IP, but to no avail.
Is there a IIS configuration to set so it will accept incomming cURL requests or is there a specific header to be sent though cURL?
Thanks.
You have a typo:
curl_setopt ($curl, CURLOPT_URL, $rurl);
Should be:
curl_setopt ($curl, CURLOPT_URL, $url);
Here's an example of a working curl request:
<?php
$url = 'http://thepointless.com:80/';
$curl = curl_init();
curl_setopt ($curl, CURLOPT_VERBOSE, true);
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt ($curl, CURLOPT_HEADER, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, false);
curl_setopt ($curl, CURLOPT_PORT , 80);
$response = curl_exec($curl);
print $response;
?>
It's just like yours, except three things:
It's connecting to port 80
It's not setting STDERR
The typo is fixed
Run cURL from the command line with --verbose to see more detailed information.
And check if ISS it's with cURL enabled:
extension=php_curl.dll
Related
When I run the below code on server it only shows the blank page and suddenly stop further execution, I also checked the cUrl on server which is installed.
Here is my code.
$ftp_server = 'ftps://'.'server/Voorraadtonen link.csv';
$ch = curl_init(str_replace(" ","%20",$ftp_server));
curl_setopt($ch, CURLOPT_URL, $ftp_server);
curl_setopt($ch, CURLOPT_USERPWD,'username'.':'.'password');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS);
//curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_PORT, 990);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($ch, CURLOPT_VERBOSE,true);
$output = curl_exec($ch);
$error_no = curl_errno($ch);
echo $output; exit;
Latest update!
You have more than 1 errors in your codes,
you are using FTPS in url which requires SSL verification, and its false in
your codes.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//Dont use try! you shouldnt use
curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
They should be true : SSL doesnt support true so they should be like following on #dharman warn in another answer.
But turning ssl true will require another setup like cacert file
etc. lik so
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
//and include cacert.pem
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
Download cacert file here : https://curl.haxx.se/docs/caextract.html
2.Your url is not a true url $ftp_server = 'ftps://'.'server/Voorraadtonen link.csv';, this url will get nothing, but it should return an error atleast in error_log file, as you said all errors reporting are enabled
3.Your code should look like this
$curl = curl_init();
$file = fopen("link.csv", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://ftp.site.com/link.csv");
//Make sure for correct url
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
//Make sure for correct url
curl_setopt($curl, CURLOPT_FILE, $file);
curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
//Make sure for your ftp credentials
curl_setopt($curl, CURLOPT_TIMEOUT, 20); //20 seconds will be enough
curl_exec($curl);
echo curl_errno($ch);
echo curl_error($ch);
curl_close($curl);
fclose($file);
1 more thing left headers should not be required but in case its required.
curl_setopt($curl, CURLOPT_HEADER, false); //Or
curl_setopt($curl, CURLOPT_HEADER, true);
Now it should work without any problem
NOTE : Example code is a working example you can edit it to your requirements
UPDATE : After modification you said you did in your codes (Still not showing us), finaly we get an error. once again I am asking you to add modified code into your question.
Error_no 28 cURL error 28: Connection timed out
the cURL 28 error occurs when the cURL request isn’t completed in a certain amount of time.
This happens when the cURL timeout value is set too low or when a firewall is blocking the cURL request.
Another possibility is a security module, for example the Apache mod_security module.
To fix the cURL error 28 you can contact your hosting provider.
So basicaly!
Your server is blocking. your credentials not match to required
credentails. SSL is required by server, but you are not setting it up.
Your function runing max of your Server Memory Limits settings.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "ftp.site.com/link.csv");
//make sure your path to file is correct
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
//make sure your login credentials correct
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
//Set timeout for connection
curl_exec($curl);
echo curl_errno($ch);
echo curl_error($ch);
//Get errors
curl_close($curl);
//Importand close curl connection.
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $xml_url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_HEADER, false);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
$xml = curl_exec($ch);
if(curl_exec($ch) === false)
{ echo curl_error($ch); }
else
{ echo 'Operation completed without any errors'; }
curl_close($ch);
return $xml;
Above code is giving below error.
Unknown SSL protocol error in connection to api.site.com:443
As per suggested by many people that below code will resolve above issue but it is not helping. Still getting same error.
curl_setopt ($ch, CURLOPT_SSLVERSION, 3);
I tried below also as per suggestions but still getting same error.
curl_setopt ($ch, CURLOPT_SSLVERSION, 'CURL_SSLVERSION_SSLv3' );
Please suggest what else I should put in code to fix this error.
Thank you,
You just ignored the SSL certificate verification, may be it will help you to resolve the problem, use the code below when requesting with CURL.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
You could check which TLS version the website uses with curl verbose mode:
curl_setopt($curl, CURLOPT_VERBOSE, true);
Where the output will be like this:
...
SSL connection using TLS1.0 / RSA_3DES_EDE_CBC_SHA1
...
Then set CURLOPT_SSL_CIPHER_LIST like this:
curl_setopt($curl, CURLOPT_SSL_CIPHER_LIST, "TLSv1");
Where "TLSv1" is the TLS version the website uses.
Changing SSL version and VERIFY_PEER/HOST in curl options didn't solve my problem, but this approach did.
You may try this code.
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $xml_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_HEADER, false);
$xml = curl_exec($ch);
if($xml === false) {
echo curl_error($ch);
}else{
echo 'Operation completed without any errors';
}
curl_close($ch);
return $xml;
Also you may use this class for curl request,
https://gist.github.com/sourovroy/1fba93188470bbe3eefa15cd17533b23
little piece of code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://image.example.jpg');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$c = curl_exec($ch);
this works from a server but wont work from another. curl_errno() returns false with error 7 - couldn't connect to host, and curl_error() say "failed to connect to %%%%%% port 80: connection timeout". But it only applies to a specific server! If I put an other image from google, it does work from both server! What can it be?
I am currently in a project and i need to put my php app connecting to a proxy.
I tried Curl in many ways, but i couldnt find out what am i doing wrong, i always get errors and more errors. All the code i found has no explanation and no commented code.
How can i connect to a proxy using PHP and make PHP search for strings on a website?
I tried this(with other proxy confs):
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, "http://proxy.example.com"); //your proxy url
curl_setopt($ch, CURLOPT_PROXYPORT, "8080"); // your proxy port number
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "username:pass"); //username:pass
$file_contents = curl_exec($ch);
curl_close($ch);
return $file_contents;
I have locally set 2 Apache Server on Port 80 and port 81 using XAMPP. Iam successfully able to access them through my browser. Currently the URL can be accessed at
http://27.4.198.225/ncmsl/check.php
and
http://27.4.198.225:81/ncmsl/check.php.
When I try to write a simple curl code for them
$ch=curl_init();
$url = "http://27.4.198.225/ncmsl/check.php";
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
curl_close($ch);
It works perfectly fine for server at port 80 but doesn't work for server at port 81, i.e.
$ch=curl_init();
$url = "http://27.4.198.225:81/ncmsl/check.php";
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
curl_close($ch);
What could be the possible reason? I have tried using CURLOPT_PORT but that also doesn't work
These URL are LIVE URL. Can anybody check whether they are successfully able to access them using thei own CURL code on their own network
Try this
curl_setopt ($ch, CURLOPT_PORT , 81);
Update code:-
see this URL:- php curl problem
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, 'http://27.4.198.225:81/ncmsl/check.php');
$store = curl_exec ($ch);
echo substr($store, 1);
curl_close ($ch);
Take a look at CURLOPT_PORT in the manual for curl_setopt()
Use this one for specify port,
curl_setopt($ch, CURLOPT_PORT, 81);
Try this:
curl_setopt($ch, CURLOPT_PORT, $_SERVER['SERVER_PORT']);