【问题标题】:Slim framework put post from other serverSlim 框架从其他服务器发布帖子
【发布时间】:2016-07-30 06:57:38
【问题描述】:

代码在同一台服务器上运行良好。

我的问题是如何在一台服务器上发布更新?

所以如果我在www.domain1.comwww.domain2.com(api)emailadres 上发帖,我该如何实现呢?我得到 404 页面。

$app = new \Slim\Slim();

$app->put('/user/update/:id/', function( $id ) use( $app ){
global $connection;
$app->response()->header("Content-Type", "application/json");

if( $id && $id > 0 ){
    $result = $connection->query( 'SELECT * FROM `users` WHERE id = '.(int)$id.' ;' );
}else{
    $result = array();
}

if ($result) {
    $post = $app->request()->put();
    $result = $connection->query( "UPDATE `users` SET email = '".$_POST['email']."' WHERE id = $id;");
    echo json_encode(array(
    "status" => (bool)$result,
    "message" => "User updated successfully"
    ));
}
else{
    echo json_encode(array(
    "status" => false,
    "message" => "User id $id does not exist"
    ));
}
});

$app->run();?>

表格:

<form action="" method="post">
    <input type="text" name="email" value=""/>
    <input type="hidden" name="_METHOD" value="PUT"/>
    <input type="submit" value="Update user"/>
</form>

【问题讨论】:

  • 我不确定我是否理解你所说的。对我来说,您有“www.domain1.com”,您的用户可以使用 html 页面访问,而“www.domain2.com”则有您的 API。我说的对吗?

标签: php slim


【解决方案1】:
if($_POST){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"http://domain.com/user/update/7/");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "email=".$_POST['email']."&_METHOD=put");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $server_output = curl_exec ($ch);
    curl_close ($ch);
}

需要发送_METHOD=put

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 2015-07-20
    • 2014-05-02
    • 2016-04-14
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    相关资源
    最近更新 更多