ksm001
ksm001

Reputation: 4022

PHP: Difficulties with sending a POST request to a .php file with curl

I have a php file let's say A.php that gets some variables by $_POST method and updates a local database.

Another php file with the name dataGather.php gathers the data in the correct form and after that it tries to send the data to the local database by using the A.php file. Note that both files are in the same directory.

The code where I use the curl functions to do the POST request is the following:

    $url = "A.php";
    $ch = curl_init();
    $curlConfig = array(
        CURLOPT_URL            => $url,
        CURLOPT_POST           => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS     => $datatopost
    );
    curl_setopt_array($ch, $curlConfig);
    $result = curl_exec($ch);
    curl_close($ch);
    echo $result;

$datatopost

is an array like the following:

$datatopost = array (
       "value1" => $val1,
       "value2" => $val2,
        etc
 }

The problem is that when I run my program I get the following result:

Fatal error: Maximum execution time of 30 seconds exceeded in 
        C:\xampp\htdocs\dataGather.php on line 97

does anyone know why this is happening? Thanks in advance

PS: The file A.php is 100% correct because I have tested it by gathering the information needed with javascript. It informs the database the way I want. Also the array $datatopost has all the information in the correct form.

Upvotes: 1

Views: 358

Answers (1)

arkascha
arkascha

Reputation: 42935

I suspect you directly run your php script without using a web server but by simply starting the script as executable. This is suggested by the fact that you have an absolute path in your error message. Whilst it is absolutely fine to run a php script like that you have to ask yourself: what does that cURL call actually make? It does not open and run the php file A.php you tried to reference. Why not? Because cURL opens URLs, not files. And without using a server that can react to url requests (ike a http server), what do you expect to happen?

The error you get is a timeout, since cURL tries to contact a http server. Since you did not specify a valid URL it most likely falls back to 'localhost'. but there is not server listening there...

Upvotes: 3

Related Questions