2

I am trying to make a CURL request to an IP address, however it just fails and I have no idea why. This is my code:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"127.0.0.1/script.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "postdata=postdata&postdata1=postdata1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);

curl_close ($ch);

However it justs returns an empty result. Any suggestions here?

  • Can you show us your script.php file ? – KubiRoazhon Feb 19 '19 at 10:16
  • `http://127.0.0.1/script.php`, or just `/script.php` as that IP is localhost. You can't omit the protocol from the URL unless using using a relative path – tshimkus Feb 19 '19 at 10:17
  • 1
    Add the `http://` protocol to your URL and maybe use [curl_error()](https://secure.php.net/curl_error) to get an idea of why it fails. – brombeer Feb 19 '19 at 10:20

1 Answers1

1

Try with:

curl_setopt($ch, CURLOPT_URL,"http://127.0.0.1/script.php");

and CURLOPT_POSTFIELDS sould be an array:

$data = array('postdata' => 'postdata', 'postdata1' => 'postdata1');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
dassoun
  • 79
  • 5