1

I'm attempting to learn how to use curl in php from a tutorial. I have one php script communicating with another. The first script is:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,"http://localhost/some_directories/testing.php");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "Hello=World&Foo=Bar&Baz=Wombat");

curl_exec ($curl);
curl_close ($curl)

And a second 'testing.php' script contains only the php tags and: var_dump($_POST);

When I run the first script I get the output: var_dump($_POST); instead of the posted values. I'm sure this is something obvious, but I'm not sure why it's happening.

Memento Mori
  • 3,327
  • 2
  • 22
  • 29
  • 1
    Are you sure you included ` – Amal Murali Dec 19 '13 at 19:34
  • Both the files do have the php tags. Sorry I should have made it clearer in the message, but I had some trouble getting them to show up. – Memento Mori Dec 19 '13 at 19:36
  • Okay. What happens when you try to access the page via your web browser: `http://localhost/some_directories/testing.php`? – Amal Murali Dec 19 '13 at 19:38
  • It prints out the contents of the file as well. Maybe it's an apache setup problem? – Memento Mori Dec 19 '13 at 19:39
  • adding the comment above, what about adding echo "
    "; var_dump($_GET); to the second script and then pinging that page with this http://localhost/some_directories/testing.php?Hello=World&Foo=Bar&Baz=Wombat
    – ssaltman Dec 19 '13 at 19:40
  • @MementoMori: How are you running the first script then? – Amal Murali Dec 19 '13 at 19:41
  • @AmalMurali from the commandline. Like 'php firstscript.php' – Memento Mori Dec 19 '13 at 19:42
  • things to check: are the files in the same directory? are there any characters at the very beginning of the file that might make the script miss the 'var_dump($_POST);' – ssaltman Dec 19 '13 at 19:44
  • @MementoMori: Load the PHP file via your browser and immediately check the webserver error log (i.e. `var/log/apache2/error.log` if you're on Unix). – Amal Murali Dec 19 '13 at 19:51
  • Thanks for your help Amal and others. It was a mistake in the httpd.conf file, loadmodule php5 was commented out. Obviously I should have noticed this earlier but I was running the files from the command line. – Memento Mori Dec 19 '13 at 19:57
  • @MementoMori: Why did you accept [this](http://stackoverflow.com/a/20690578/1438393) answer? What you said was suggested in [Eduardo's link](http://stackoverflow.com/a/20690449/1438393) – Amal Murali Dec 19 '13 at 23:48
  • @AmalMurali Switched it up. I just wanted to mark something to make sure more people didn't try to answer since I solved it. In all honesty you really deserve the credit. Anyway thanks! – Memento Mori Dec 20 '13 at 01:56

2 Answers2

1
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,"http://localhost/some_directories/testing.php");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "Hello=World&Foo=Bar&Baz=Wombat");

$response = curl_exec ($curl);
curl_close ($curl);

echo $response;
?>

testing.php

<?php 
var_dump($_REQUEST);

Check this too... if none of your PHP code is running - https://stackoverflow.com/a/5121589/781251

Community
  • 1
  • 1
Eduardo Stuart
  • 2,869
  • 18
  • 22
0

You forgot to set a true flag for CURLOPT_RETURNTRANSFER

Try this instead, its a lot cleaner, and breaks your parameters up a bit

$url = 'http://localhost/some_directories/testing.php';

$ch = curl_init($url);
curl_setopt_array($ch, array(
    CURLOPT_POST            => 1,
    CURLOPT_RETURNTRANSFER  => 1,
    CURLOPT_POSTFIELDS      => http_build_query(array(
                                'Hello' => 'World',
                                'Foo'   => 'Bar',
                                'Baz'   => 'Womabt',
                            )),
));

// error
if (FALSE === ($data = curl_exec($ch)))
{
    print_r(curl_error($ch));
}

curl_close($ch);

print_r($data);
ehime
  • 8,025
  • 14
  • 51
  • 110