How to use PHP to POST to a web page then get the results back, locally

Posted by Patrick Gates on Stack Overflow See other posts from Stack Overflow or by Patrick Gates
Published on 2010-05-27T03:24:07Z Indexed on 2010/05/27 3:31 UTC
Read the original article Hit count: 182

Filed under:
|
|

I have a page on my web server that is PHP that is set to do this

if ($_POST['post'] == true) { echo: 'hello, world'; }

I want to create a page that calls to that page, posts "post" equal to "true" and then returns the value "hello, world". I have a script that works, but only if the pages are on different servers. Unfortunately, both of these pages are on the same server, so, heres my code, and I'm hoping you guys can help me, Thank you :)

function post($site, $post) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$site);
    curl_setopt($ch, CURLOPT_FAILONERROR,1);
    curl_setopt ($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    $retValue = curl_exec($ch);
    curl_close($ch);
    return $retValue;
}

echo post('data.php', 'post=true');

© Stack Overflow or respective owner

Related posts about php

Related posts about post