Curl function not working in XAMPP 1.8.0 - php

I am using Windows 7. I have installed XAMPP 1.8.0 and also enabled the CURL functionality. But, I am still not able to crawl web pages.
I used the following code segment:
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.google.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
And when I executed the program, the error message was:
Fatal error: Maximum execution time of 30 seconds exceeded in
C:\xampp\htdocs\testing\curl.php on line 14
Line 14: curl_close($ch)
Why I am getting such an error? How can I debug it? Please help me.

Add set_time_limit($seconds); in your page
also
add curl_setopt ($ch, CURLOPT_TIMEOUT, 60); and try

Related

How to fix Connection timed out, cURL error 28?

I bought a jobs portal script, I've successfully installed it and when I try to register I get this error:
cURL error 28: Connection timed out after 2013 milliseconds (see
http://curl.haxx.se/libcurl/c/libcurl-errors.html)
I asked support and they said:
you need to increase read_timeout and timeout. The error is clear, you
don’t have enough time to get the response. increase time in php.ini
I tried increasing max_execution_time, default_socket_timeout in php.ini both to 500, but I'm getting the same error. Then I tried manually adding read_timeout=500 and timeout=500 and again the same error.
What should I do?
CURLE_OPERATION_TIMEDOUT (28)
Operation timeout. The specified time-out period was reached according to the conditions
You can set the total time of the cURL transfer using:
curl_setopt($ch, CURLOPT_TIMEOUT, 500);
Where 500 is the maximum number of seconds to allow cURL functions to execute.
Here is an example of initializing a new cURL session and fetching a web page:
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 500);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
Maybe you should enable stream_ socket_ server
Use the below mention in your curl request
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 400); //timeout in seconds

cURL error 28 - Connection timed out after x milliseconds

Yep. I know there is some alike questions about this err but i was read all of this and it not resolve my problem so:
My php code:
$url = 'example.domain.com/path/file.php'
$string = 'param=5';
$ch = curl_init();
// CURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'Content-Length: ' . strlen($string)));
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
return curl_exec($ch);
I check errors this way:
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
if($curl_errno > 0) {
echo "cURL Error ($curl_errno): $curl_error\n";
}
$curl_errorno is 28 and
curl_error is Connection timed out after 10001 milliseconds
Please help or get some clue what I can check.
from localhost or other server is it also working (curl or file_get_content)... is there any hint?
from local machine i get cURL ok response via php ~4sec
from server (host server) shell i get error 7 failed to connect to example.domain.com port 80: connection timed out
if in php try file_get_contents (from host server) - no response
URL - is accessible from browser (direct php file)
If i create ajax request - response is ok
If i trying add to url http or https - always same error
if i trying set limit to 30 sec. same result
I was having this problem, this is because the API you are trying to send the request does not accept the sending of many products at the same time. Then you need to put the PHP sleep function, so between each sent line a range is defined. Add sleep(seconds of interval); in your code.
You have to increase CURLOPT_TIMEOUT or remove timeout of cURL execution at all

cURL shows blank page

my curl code show me a blank page
<?
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL,"http://mysite/scripts/showsomething.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
$core = explode('<!--- BEGIN -->', $result);
$main = explode('<!--- END -->', $core[1]);
echo $main[0];
?>
this code works fine on localhost, but not on server...
There can be several reasons behind your problem.
1) Change <? into <?php and see whether it works or not.
2) For a short test, run this code from your server and check whether it shows you the output or not.
<?php
echo "sabuj";
?>
3) Some site seek for useragent string on their website request. When they found no useragent, they use to return blank page. You can overcome this with setting an useragent like below.
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
4) Another thing you can do is, access the server with ssh client(if you have any) and run the url with wget tool and see whether you can download your page or not.
wget "http://yoursite/page/blabla/...php"
5) Finally, run your curl code with verbose mode enabled. It will help you to debug your curl requests.
curl_setopt($ch, CURLOPT_VERBOSE,true);
Working Code...
$ch = curl_init();
$timeout = 10;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
var_dump($data);
Hope it helps...
Turn your error reporting all the way to 11.
error_reporting(E_ALL);
This will tell you a little more about the issue you are facing. The most common culprit when working with curl on a dev environment and moving to a server is a missing curl package.
You can check to see if you have curl installed by doing the following:
if(!function_exists('curl_version')) {
throw new Exception('Curl package missing');
}
Thus "PHP Fatal error: Call to undefined function curl_init() in..." is a very common error that is thrown.
Some additional debugging tips are to..
print_r the response of curl_exec($handle);
print_r curl_error($handle) this will give you a curl error code.
setup a curl proxy using set_opt and CURLOPT_PROXY, set the value to your ip:port, open the port 8888 on your router and install charles proxy. This will show you an exact printout of the request and response.
I've read the previous answers and it seems that your code is fine but problem is connectivity to remote server, please try this:
<?
//debug
error_reporting(E_ALL);
ini_set('display_errors', 1);
//debug end
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL,"http://mysite/scripts/showsomething.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo result;
?>
what does it output ?
Notes:
Do you administer the other server?
if not, is there a possibility that your ip was blocked by the remote server?
Does script http://mysite/scripts/showsomething.php contains any errors ?
If you're able to edit it, please enable php errors on showsomething.php by adding the following code at the top of it :
error_reporting(E_ALL);
ini_set('display_errors', 1);
I've solved the problem by adding the following code.
The problem is SSL.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
I hope it helps.

Using CURL, gives 500 Internal Server Error

I am using CURL and I am getting a 500 internal server error. I am not using the user agent option, could this be causing the issue?
This is the snippet
$current_url="http://localhost/mysite/entercode.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $current_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'variable1=abc&variable2=123');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
The same thing just happened to me.
A 500 Internal Server error when i tried to execute $ch = curl_init();. It turns out php curl wasn't installed on my server. I installed it and presto my code worked!
Check this out for how to install PHP CURL
entercode.php is throwing an error related to your input. I suggest you check the server error log. Your curl implementation is fine.

How to set a timeout on PHP5 curl calls? Published CURL options do not seem to work

We've written a script that pulls data from an external server. If the server goes down we don't want our server waiting for the data since we process a lot of data and we don't want it bogged down. To address this, we're trying to timeout our curl calls if they take more than a couple hundred milliseconds.
I found some documentation saying that CURLOPT_TIMEOUT_MS and CURLOPT_CONNECTTIMEOUT_MS should be available in my version of php and libcurl, but it does not seem to be timing out, even if I set the timeout to 1ms.
$url = "http://www.cnn.com;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER,0); //Change this to a 1 to return headers
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 1);
$data = curl_exec($ch);
curl_close($ch);
Does anyone know what we're doing wrong or another way to do this?
saw this in unresponsive dns server and curl multi timeouts not working:
"...We have had some times where a
site that we pull information has had
dns server become unresponsive. When
this happens the timeouts set in curl
(php bindings) do not work as
expected. It times out after 1min 14
sec with "Could not resolve host:
www.yahoo.com (Domain name not found)"
To make this happen in test env we
modify /etc/resolv.conf to have a
nameserver that does not exist
(nameserver 1.1.1.1). No mater what
they are set at
(CURLOPT_CONNECTTIMEOUT, CURLOPT_CONNECTTIMEOUT_MS
, CURLOPT_TIMEOUT, CURLOPT_TIMEOUT_MS)
they don't timeout when we cant get
to the DNS server. I use curl_multi
because i we have multiple sources
that we pull info from at the same
time. The example below makes one
call for example simplicity. And as a
side note curl_errno does not return
an error code even though there was an
error. Not sure why..."

Categories