如何在PHP中使用卷曲POST数据?(how to POST data using Curl in

2019-10-17 16:54发布

我已经创造了restler框架web服务,我想插入从我所创建的客户端的lib 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。 请帮忙。

Answer 1:

你不应该映射所有参数的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);
}

然后你卷曲的例子应该做工精细

$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);


Answer 2:

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

注:取消该行可能。

请参阅使用卷曲后数据的例子。

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;
    }

Checkout的链接,了解更多的信息,

执行一个HTTP POST使用PHP CURL

提交表单后用PHP和卷曲

张贴到URL使用卷曲和PHP

安装使用PHP一个简单的卷曲连接到POST数据

编辑:

Restler总是返回错误404未找到

可以帮助你。



文章来源: how to POST data using Curl in php?