Can I POST from one PHP script to another using phpQuery, or if possible, just plain PHP?

925 Views Asked by At

There are numerous examples of being able to POST a variable from one PHP script to another.

I want the first script to POST to the second script, but to keep the first script still running. The files are crawler.php and links.php. How do I do this?

1

There are 1 best solutions below

1
On

Use cURL

<?php 
// crawler.php
$url = 'localhost/links.php'; // Change me to what ever
$fields = array('foo' => 'bar');

//url-ify the data for the POST
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

See http://davidwalsh.name/execute-http-post-php-curl

Edit:

I just realised you wanted an async call.

In that case, you can look into pcntl fork http://php.net/manual/en/book.pcntl.php

Or How do I make an asynchronous GET request in PHP?