1

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?

Adam Lear
  • 38,111
  • 12
  • 81
  • 101
Thomas Foster
  • 1,303
  • 1
  • 10
  • 23
  • 2
    This is the first time I've seen a reporter from the Daily Mail write a question on Stack Overflow. – Hammerite Oct 25 '11 at 06:11
  • Sorry! I wa slightly annoyed that last time I asked a question, 20 people commented saying that it was too hard, or that you couldn't (you can), or that they needed more information than was humanly possible to include. – Thomas Foster Oct 25 '11 at 06:16
  • Can you desribe exatly what you are trying to do and specifically what you mean by "post" (ie. a second http request, passing through a socket, etc..) – prodigitalson Oct 25 '11 at 06:17
  • @tandu I've seen a solution in a Google search that did that, but it made no sense to me at all. – Thomas Foster Oct 25 '11 at 06:17
  • `to keep the first script still running` concurrent php programming?) – k102 Oct 25 '11 at 06:20
  • @Thomas: can you post the link? – prodigitalson Oct 25 '11 at 06:22
  • @prodigitalson a second HTTP request. And what link? k102, I haven't heard about that, GOOGLE SESSION! – Thomas Foster Oct 25 '11 at 06:24
  • Two ways that I'd consider: CURL (http://uk.php.net/manual/en/book.curl.php) alternatively output an AJAX request to the browser to run another PHP script. – David Barker Oct 25 '11 at 06:25
  • @DavidBarker I was trying to keep it all on the server, sorry should have made it more clear! – Thomas Foster Oct 25 '11 at 06:29
  • @TiesonT. Sorry, I was in a bad mood after my last experience on this site... – Thomas Foster Oct 25 '11 at 06:29
  • @Thomas Foster Fair enough, just thought it may explain why you weren't getting much in the way of suggestions. Don't have to apologize to me... :) – Tieson T. Oct 25 '11 at 06:36
  • Asking such pointless questions you will always have bad experience on this site. To have a good experience, one have to put much more efforts, asking not 2-liner question making very little sense but posting whole problem description in terms that makes sense. – Your Common Sense Oct 27 '11 at 03:11

1 Answers1

2

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?

Community
  • 1
  • 1
Petah
  • 45,477
  • 28
  • 157
  • 213
  • Does that require for something to be sent back to the original script? (the edit) I just didn't want the first script to stop running after I had sent the request, which is what the examples I saw did. The cURL will work I guess :) – Thomas Foster Oct 25 '11 at 07:33