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 not found

可以帮助你。

于 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 回答