【问题标题】:how to POST data using Curl in php?如何在 php 中使用 Curl 发布数据?
【发布时间】:2013-02-15 10:08:05
【问题描述】:

我已经在 restler 框架中创建了 web 服务,我正在尝试使用 curl 从我创建的客户端库中的 php 中插入数据。

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。请帮忙。

【问题讨论】:

  • 可能是没有获取到url路径。
  • 我正在使用 GET 方法获得成功。
  • 嗯,404 只是一个未找到的响应。您是否检查过 URL 是否正确?在浏览器或命令行中尝试。
  • 所以使用GET 如果它工作正常。:)

标签: php web-services curl restler


【解决方案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;
    }

查看一些链接以获取更多信息,

Execute a HTTP POST Using PHP CURL

Submitting a form post with PHP and CURL

Post to a URL Using cURL and PHP

Setup a simple cURL connection to POST data using PHP

编辑:

Restler always returning error 404 not found

可以帮到你。

【讨论】:

  • 你有没有在 restler 中定义过 POST 参数?我怀疑我可能不正确。
  • @ketulshah 查看我的答案stackoverflow.com/questions/10506060/…
  • @ketulshah 我从未使用过restler框架,但这不是问题。
  • @ketulshah 你在restler 框架中更新了版本吗?并在编辑一个链接后检查我的答案。
  • 是的,我正在使用restler的v 3
【解决方案2】:

您不应该将所有参数都映射到 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);

【讨论】:

  • 感谢您确认答案:)
猜你喜欢
  • 2012-06-20
  • 2018-03-29
  • 2011-12-28
相关资源
最近更新 更多