1

restler フレームワークで Web サービスを作成し、作成したクライアント ライブラリから php で curl を使用してデータを挿入しようとしています。

api.php

/**
     * Manually routed method. we can specify as many routes as we want
     *
     * @url POST addcomment/{token}/{email}/{comment}/{story_id}
     */
    function addComment($token,$email,$comment,$story_id){
        return $this->dp->insertComment($token,$email,$comment,$story_id);
    }

クライアントからのテスト目的:testing.php

$data_string = "token=900150983cd24fb0d6963f7d28e17f72&email=kamal@gmail.com&comment=commentusingcurl&story_id=2";                                                                                   

$ch = curl_init('http://localhost/Restler/public/examples/news/addcomment');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
//curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-Type : text","Content-lenght:".strlen($data_string)));                                                                                                                                     
$result = curl_exec($ch);

しかし、それは404を投げています。助けてください。

4

2 に答える 2

1
//curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-Type : text","Content-lenght:".strlen($data_string)));

注:行のコメントを外してください。

curl を使用した投稿データの例を参照してください。

function post($requestJson) {

        $postUrl = $this->endpoint."?access_token=".$this->token;

        //Get length of post
        $postlength = strlen($requestJson);

        //open connection
        $ch = curl_init();

        //set the url, number of POST vars, POST data
        curl_setopt($ch,CURLOPT_URL,$postUrl);
        curl_setopt($ch,CURLOPT_POST,$postlength);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$requestJson);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($ch);

        //close connection
        curl_close($ch);

        return $response;
    }

詳細については、いくつかのリンクをチェックしてください。

PHP CURL を使用して HTTP POST を実行する

PHP と CURL を使用したフォーム投稿の送信

cURL と PHP を使用して URL に投稿する

PHP を使用して POST データへの単純な cURL 接続をセットアップする

編集:

Restler は常にエラー 404 を返しますが見つかりません

あなたを助けるかもしれません。

于 2013-02-15T10:20:44.127 に答える
0

すべてのパラメータをURLにマッピングするべきではありません。

/**
 * Manually routed method. we can specify as many routes as we want
 *
 * @url POST addcomment/{token}/{email}/{comment}/{story_id}
 */
function addComment($token,$email,$comment,$story_id){
    return $this->dp->insertComment($token,$email,$comment,$story_id);
}

そうすることで、APIは次のようなURLのみを受け入れることができます

http://localhost/Restler/public/examples/news/addcomment/900150983cd24fb0d6963f7d28e17f72/kamal@gmail.com/commentusingcurl/2

APIメソッドをに変更します

/**
 * Manually routed method. we can specify as many routes as we want
 *
 * @url POST addcomment
 */
function addComment($token,$email,$comment,$story_id){
    return $this->dp->insertComment($token,$email,$comment,$story_id);
}

次に、cURLの例は正常に機能するはずです

$data_string = "token=900150983cd24fb0d6963f7d28e17f72&email=kamal@gmail.com&comment=commentusingcurl&story_id=2";

$ch = curl_init('http://localhost/Restler/public/examples/news/addcomment');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
//curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-Type : text","Content-lenght:".strlen($data_string)));                                                                                                                                     
$result = curl_exec($ch);
于 2013-02-16T07:08:49.267 に答える