【问题标题】:Processing POST data server side处理POST数据服务器端
【发布时间】:2016-02-19 07:02:55
【问题描述】:

我目前正在测试将 POST 数据从网页传递到另一个 URL,并正在尝试研究如何通过 POST 脚本将值从一个页面传递到 URL,然后该 URL 将通过服务器端脚本处理 POST 数据.我正在使用以下 CURL 启动 POST,其中 xxxyyy.com/post_processor.php 是处理脚本的 url。

<?php

//set POST variables
$url = 'http://xxxyyy.com/post_processor.php';
$fields = array(
    'lname' => 'smith',
    'fname' => 'peter',
);

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

?>

这会按预期发布数据(我已经在 http://requestb.in/12lfg7x1 中对其进行了测试)但是我在处理任何发布的信息时都遇到了问题。

目前,服务器端的处理脚本只包含一个 if 语句,用于查看是否收到任何 post 数据,如果收到则写入数据库:

$entityBody = file_get_contents('php://input');  

if (!empty($entityBody))
    {
        // handle post data

    global $wpdb;

            $table_name = $wpdb->prefix . 'post_data_1';
            $wpdb->insert( 
            $table_name, 
            array( 
                'time' => current_time( 'mysql' ), 
                'post_data_recieved' => 'ok',
            )
        );

}

这种组合目前不起作用,即我加载了初始脚本,但没有任何数据被 POST,但是没有数据存储在远程数据库中。但是,如果我使用表单将 POST 数据传递给脚本,这确实有效,但是如果我在没有重定向的情况下传递发布数据,则它不起作用。谁能告诉我我在这里做错了什么?

谢谢,

马特

【问题讨论】:

  • 你从哪里得到$entityBody
  • $entityBody = file_get_contents('php://input');

标签: php post curl


【解决方案1】:

这与最初发布的内容基本相同,并且返回值成功打印到屏幕上。

数据库没有被更新的事实让我认为post_processor.php脚本中可能没有数据库连接,所以post_processor.php拥有所有必要的包含文件以确保$wpdb可用?

<?php
    $url = 'http://xxxyyy.com/post_processor.php';
    $url = 'https://localhost/target.php';


    $fields = array(
        'lname'     => 'smith',
        'fname'     => 'peter'
    );
    /* prepare the data for transmission as a urlencoded string */
    $data=http_build_query( $fields );



    $ch = curl_init();

    if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
        /* my server runs https so I need these lines*/
        curl_setopt( $ch, CURLOPT_CAINFO, realpath('c:/wwwroot/cacert.pem') );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    }

    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Length: '.strlen( $data ) ) );

    $result = curl_exec( $ch );
    $info = curl_getinfo( $ch );
    curl_close( $ch );

    print_r( $result );
?>



<?php

    $data=file_get_contents('php://input');
    print_r( $data );

    /* what would this show ? Object hopefully I guess */
    echo '$wpdb = '.gettype( $wpdb );
?>

outputs:
lname=smith&fname=peter

【讨论】:

    猜你喜欢
    • 2014-10-06
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多