dongmoxin7111 2010-05-27 03:24
浏览 43

如何使用PHP POST到网页然后在本地获取结果

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');
  • 写回答

3条回答 默认 最新

  • dqrnsg6439 2010-05-27 03:33
    关注

    Since you have control over both pages, why not change your conditional? (adding the whole $_POST in this example incase there are more conditionals)

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

    Then, you can just set the var and include the file:

    $postData = array('post' => true);
    include ( 'data.php' );
    
    评论

报告相同问题?