使用PHP中的CURL发布JSON编码和普通字符串数据

时间:2014-10-18 08:25:10

标签: php json curl

我通常使用PHP中的CURL发送一个字符串数组,如下所示:

$data = array(
"key1" => $value,
"key2" => $value,
"key3" => $value,
"key4" => $value     

);

然后,在其他curl_setop设置中,发布使用:

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

一切正常。但是现在除了这些字符串之外,我还有一个JSON编码的数据集,我想同时发布它。 JSON看起来像这样:

Array ( [ad_info] => {"MoPubAdUnitInteractions":"a","MoPubAdUnitConversations":"b","MoPubAdUnitGroups":"c"} )

我想通过设置一个标题来说明我将如何传递JSON,我想出了如何做到这一点:

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

但是,我可以简单地为帖子添加另一行,但在这种情况下,JSON编码的值如下:

curl_setopt($curl, CURLOPT_POSTFIELDS, $json_data);

我有点在黑暗中拍摄,有关我应该如何思考的任何建议?

好的,这是完整的例子:

从处理表格帖子:

$community_id = $_POST["communityid"];
$community_name = $_POST["communityname"];
$community_apns_name = $_POST["communityapnsname"];
$community_download_url = $_POST["communitydownloadurl"];
$community_open_tagging = $_POST["communityopentagging"];
$community_pull_content = $_POST["communitypullcontent"];
$community_push_content = $_POST["communitypushcontent"];
$community_ad_info = json_encode($_POST["ad_info"]);    

$data = array(
  "name" => $community_name,
  "apns_name" => $community_apns_name,
  "download_url" => $community_download_url,
  "open_tagging" => $community_open_tagging, 
  "pull_content" => $community_pull_content,
  "push_content" => $community_push_content,                  
);

$json_data = array("ad_info" => $community_ad_info);
$api_querystring = $gl_app_api_url . "communities";

$response = CallAPI('PATCH', $api_querystring, $data, $device_id = null, $community_id = null, $json_data);

PHP中的函数我打电话来做CURL:

function CallAPI($method, $url, $data = false, $device_id = false, $community_id = false, $json_data = false) {

    if (!$community_id) { // IF NO COMMUNITY ID IS PROVIDED
        global $gl_community_id;
        $community_id = $gl_community_id;
    }
    $curl = curl_init();

    switch ($method)
    {
        case "POST":
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');

            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');

            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        case "PATCH":
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');

            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }

    // Optional Authentication:
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, "XXXX:XXXXX");

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    // Disable the SSL verificaiton process
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    if ($device_id)
        curl_setopt($curl, CURLOPT_HTTPHEADER, array("device_id:" . $device_id));

    if ($community_id)
        curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-Afty-Community:" . $community_id));         

    if ($json_data)
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($curl, CURLOPT_POSTFIELDS, $json_data);

    // Confirm cURL gave a result, if not, write the error
    $response = curl_exec($curl);

    if ($response === FALSE) {
        die("Curl Failed: " . curl_error($curl));
    } else {
        return $response;
    }
}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

你使用错误的方法。设置CURLOPT_POSTFIELDS选项两次不会产生预期的结果,因为每次设置调用都会丢弃前一个设置的效果。

而不是这样,您必须在传递给{{1}之前将额外数据($community_ad_info)附加到主POST数据($data选项。

CURLOPT_POSTFIELDS

答案 1 :(得分:0)

这当然未经测试,但它应该做你需要的。

function CallAPI($method, $url, $data = false, $device_id = false, $community_id = false, $json_data = false) {

if (!$community_id) { // IF NO COMMUNITY ID IS PROVIDED
    global $gl_community_id;
    $community_id = $gl_community_id;
}
$curl = curl_init();

switch ($method)
{
    case "POST":
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));

        if ($data)
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        break;
    case "PUT":
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');

        if ($data)
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        break;
    case "PATCH":
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');

        if ($data)
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        break;
    default:
        if ($data)
            $url = sprintf("%s?%s", $url, http_build_query($data));
}

// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "XXXX:XXXXX");

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// Disable the SSL verificaiton process
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

if ($device_id)
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("device_id:" . $device_id));

if ($community_id)
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-Afty-Community:" . $community_id));         



// Confirm cURL gave a result, if not, write the error
$response = curl_exec($curl);

if ($response === FALSE) {
    die("Curl Failed: " . curl_error($curl));
} else {
    return $response;
}

}

现在是实际逻辑

$community_id = $_POST["communityid"];
$community_name = $_POST["communityname"];
$community_apns_name = $_POST["communityapnsname"];
$community_download_url = $_POST["communitydownloadurl"];
$community_open_tagging = $_POST["communityopentagging"];
$community_pull_content = $_POST["communitypullcontent"];
$community_push_content = $_POST["communitypushcontent"];
$community_ad_info = json_encode($_POST["ad_info"]);    

$data = array(
  "name" => $community_name,
  "apns_name" => $community_apns_name,
  "download_url" => $community_download_url,
  "open_tagging" => $community_open_tagging, 
  "pull_content" => $community_pull_content,
  "push_content" => $community_push_content,                  
  "ad_info"      => $community_ad_info
);

$api_querystring = $gl_app_api_url . "communities";

$response = CallAPI('PATCH', $api_querystring, $data, $device_id = null, $community_id = null);

这里要注意的是,

新的内容tyoe标题告诉它作为表单发布处理。

删除$ json_data数组,“ad_info”键不只是添加到$ data数组中

当你在另一边处理时,你可以像这样访问ad_info

$ad_info = json_decode($_POST['ad_info']);

您可以使用

访问其他字段
$apns_name = $_POST['apns_name'];