PHP Curl POST方法

时间:2015-08-20 12:33:03

标签: php curl

我使用CURL在URL上提交表单,但没有得到任何响应,也没有收到任何卷曲错误。

使用以下参数:

  • $param变量包含数组
  • $accept变量包含值" application/vnd.citrix.g2wapi-v1.1+json"
  • $contentType包含值" application/json"

<?php
function httpPost($url,$params,$accept,$contentType)
{
   try{
        $post_data=http_build_query($params);
        //print_r($post_data);

        //  Initiate curl
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        // Set the url
        curl_setopt($ch, CURLOPT_URL,$url);

        curl_setopt($ch,CURLOPT_POST, true);
        curl_setopt($ch,CURLOPT_POSTFIELDS, $post_data);

        // Will return the response, if false it print the response
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Accept:".$accept,
        "Content-Type:".$contentType
        ));

        // Execute
        $result=curl_exec($ch);

        if ($result==false)
        {
            print_r('Curl error: ' . curl_error($result));
        }

        // Closing
        curl_close($ch);

        // Will dump a beauty json :3
        var_dump(json_decode($result, true));
    } // end try

    catch(Exception $e){
        echo $e->getMessage();
        return $e->getMessage();
    }

} // end httpPost
?>

0 个答案:

没有答案