使用curl_setopt($ ch,CURLOPT_POSTFIELDS,$ data)发送数据时的Laravel-4 notfoundhttpexception

时间:2014-05-16 04:34:34

标签: web-services rest laravel-4

当我以curl发送数据时,我收到notfoundhttpexception以及以下详细信息

Server/Request Data
REDIRECT_STATUS     200
HTTP_HOST   localhost:81
HTTP_ACCEPT     */*
CONTENT_LENGTH  15
CONTENT_TYPE    application/x-www-form-urlencoded
SystemRoot  C:\windows
COMSPEC     C:\windows\system32\cmd.exe
PATHEXT     .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
WINDIR  C:\windows
SERVER_SIGNATURE    
SERVER_SOFTWARE     Apache/2.4.4 (Win32) PHP/5.4.16
SERVER_NAME     localhost
SERVER_ADDR     ::1
SERVER_PORT     81
REMOTE_ADDR     ::1
DOCUMENT_ROOT   D:/wamp/www
REQUEST_SCHEME  http
CONTEXT_PREFIX  
CONTEXT_DOCUMENT_ROOT   D:/wamp/www
SERVER_ADMIN    admin@example.com
SCRIPT_FILENAME     D:/wamp/www/HRMS/public/index.php
REMOTE_PORT     61767
REDIRECT_URL    /HRMS/public/services/test
GATEWAY_INTERFACE   CGI/1.1
SERVER_PROTOCOL     HTTP/1.1
REQUEST_METHOD  POST
QUERY_STRING    
REQUEST_URI     /HRMS/public/services/test
SCRIPT_NAME     /HRMS/public/index.php
PHP_SELF    /HRMS/public/index.php
REQUEST_TIME_FLOAT  1400157353.813
REQUEST_TIME    1400157353

在下面找到代码:

public function getData() {
    $data=http_build_query(array("purpose"=>"Testing"));

    $url = "http://localhost:81/HRMS/public/services/test";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_HEADER,false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    $result = curl_exec($ch);

    return View::make("userlogin")->with("result",$result);
}


public function test() {

    $jsonData = array("Purpose" => "Testing Web Service", "Web Service" => "REST", "Status" => "Success");

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

    if ( $input ) {
    $response = Response::make($jsonData, 200);
    $response->header('Content-Type', "application/json");
    }else{
    $data = array("input" => "Not Testing");
    $response = Response::make($data, 200);
    $response->header('Content-Type', "application/json");
    }
    return $response;
    $input = file_get_contents('php://input');
    return "$input TEST Work";

}

但是当我发送数据删除帖子字段" CURLOPT_POSTFIELDS"时,它的工作正常。当我包括" CURLOPT_POSTFIELDS"它抛出notfoundhttp例外。

这个问题的原因是什么?请指导。

1 个答案:

答案 0 :(得分:0)

您可以将路由更改为post,因为您正在将curl请求设置为HTTP POST。

Route::post(...);