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


Post JSON encoded and normal string data using CURL in PHP

我通常在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;
    }
}

非常感谢您的帮助。

您使用了错误的方法。两次设置CURLOPT_POSTFIELDS选项并不会导致您预期的结果,因为每次设置调用都会丢弃前一次的效果。

相反,在将额外数据($community_ad_info)传递给CURLOPT_POSTFIELDS选项之前,必须将其附加到主POST数据($data

...
$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
);
$response = CallAPI('PATCH', $api_querystring, $data, $device_id = null, $community_id = null);
...

这当然是未经测试的,但它应该可以满足您的需要。

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头告诉将其作为表单post处理。

删除$json_data数组后,"ad_info"键不仅添加到$data数组中

当你在另一边处理这个时,你可以访问类似的ad_info

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

您可以使用访问其他字段

$apns_name = $_POST['apns_name'];