Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send http request with CURL to local file

Tags:

php

curl

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

like image 270
Marco Avatar asked Jul 03 '11 01:07

Marco


1 Answers

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
like image 142
GreensterRox Avatar answered Sep 23 '22 05:09

GreensterRox