6

hello i need to send http request to a local file using the file name it self not the full http path for example

    <?php
$url = 'http://localhost/te/fe.php';
// The submitted form data, encoded as query-string-style
// name-value pairs
$body = 'monkey=uncle&rhino=aunt';
$c = curl_init ($url);
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c);
?>

i need to do it to become like this

<?php
$url = 'fe.php';
// The submitted form data, encoded as query-string-style
// name-value pairs
$body = 'monkey=uncle&rhino=aunt';
$c = curl_init ($url);
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c);
?>

when i tried to do the second example it doesn't make anything. is there any solution using the Curl or even any other method ? thank you

Marco
  • 842
  • 6
  • 18
  • 42

3 Answers3

5

As of PHP 5.4.0, the CLI SAPI provides a built-in web server. You could simply serve up the directory that your fe.php script lives in and then run your script which curls this.

From the command line change to the directory and run:

cd /path/to/script/te/
php -S localhost:8000

Now amend your code so that it connects on port 8000:

<?php
$url = 'http://localhost:8000/fe.php';
// The submitted form data, encoded as query-string-style
// name-value pairs
$body = 'monkey=uncle&rhino=aunt';
$c = curl_init ($url);
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c);
?>

Now run your script e.g.:

php -f /path/to/curl/script.php
GreensterRox
  • 6,432
  • 2
  • 27
  • 30
0

I've managed of calling a local url by using the file:// call. Unfortunately, I'd like to be able to use a relative path for it to be easier to maintain from within the project files, but you can do this:

Here I'm assuming that I'm using my WAMP, but that the solution translates for servers too:** http://te/fe.php** would translate to (assuming it were in wamp) to: file:///c:/wamp/www/te/fe.php

Prusprus
  • 7,987
  • 9
  • 42
  • 57
0
<?php
$url = 'fe.php';
// The submitted form data, encoded as query-string-style
// name-value pairs

-> USE below <-
$varArr = array(
'monkey' => 'uncle',
'rhino' => 'aunt'
);
$body = http_build_query($varArr); // this will build query string

-> INSTED OF below <-
$body = 'monkey=uncle&rhino=aunt';

$c = curl_init ($url);
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c);
?>
Ali
  • 1
  • 3