curl到php customrequest

时间:2016-11-28 07:08:40

标签: php json curl

嗨,有人可以告诉我我的代码有什么问题

   foreach($campinfo['pathsGroups'] as $datacamp){
                    echo $datacamp['active'];
                    $xh = curl_init();

                    curl_setopt($xh, CURLOPT_URL, "https://core.voluum.com/campaigns/" . $id);
                    curl_setopt($xh, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt($xh, CURLOPT_POSTFIELDS, "$datacamp['active']= ");
                    curl_setopt($xh, CURLOPT_CUSTOMREQUEST, "PUT");

                    $head = array();
                    $head[] = "Cwauth-Token: " . $tok; 
                    curl_setopt($xh, CURLOPT_HTTPHEADER, $head);

                    $res = curl_exec($xh);
                    if (curl_errno($xh)) {
                        echo 'Error:' . curl_error($xh);
                    }

我试图将active的值从true更改为false 这是要改变的数据:

Array
(
    [pathsGroups] => Array
        (
            [0] => Array
                (
                    [condition] => Array
                        (
                            [country] => Array
                                (
                                    [predicate] => MUST_BE
                                    [countryCodes] => Array
                                        (
                                            [0] => IQ
                                        )

                                )

                            [customVariableConditions] => Array
                                (
                                    [0] => Array
                                        (
                                            [predicate] => MUST_NOT_BE
                                            [index] => 0
                                            [texts] => Array
                                                (
                                                    [0] => 
                                                    [1] => Unknown
                                                    [2] => unknown
                                                )

                                            [text] => 
                                        )

                                    [1] => Array
                                        (
                                            [predicate] => MUST_NOT_BE
                                            [index] => 1
                                            [texts] => Array
                                                (
                                                    [0] => Unknown
                                                    [1] => 
                                                    [2] => unknown
                                                )

                                            [text] => Unknown
                                        )

                                    [2] => 
                                    [3] => 
                                    [4] => 
                                    [5] => 
                                    [6] => 
                                    [7] => 
                                    [8] => 
                                    [9] => 
                                )

                        )

                    [paths] => Array
                        (
                            [0] => Array
                                (
                                    [weight] => 100
                                    [active] => 1
                                    [landers] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [lander] => Array
                                                        (
                                                            [id] => 
                                                            [namePostfix] => 
                                                            [name] => Global 
                                                        )

                                                    [weight] => 100
                                                )

                                        )

                                    [offers] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [offer] => Array
                                                        (
                                                            [id] => 
                                                            [name] => 
                                                            [namePostfix] => 
                                                        )

                                                    [weight] => 100
                                                )

                                        )

                                )

                        )

                    [active] => 1 //this is what iam trying to change to false or null or 0
                )

        )



)

我希望将active设置为false以将其关闭是否可能?我不知道这是否是发送改变请求的正确方法。有人可以帮助我,我真的陷入了这一部分。

1 个答案:

答案 0 :(得分:0)

也许有帮助

<?php    
$xh = curl_init();
curl_setopt($xh, CURLOPT_URL,"your URL");
curl_setopt($xh, CURLOPT_POST, 1);
curl_setopt($xh, CURLOPT_POSTFIELDS,
            "postvar1=value1&postvar2=value2&postvar3=value3");

// in real life you should use something like:
// curl_setopt($xh, CURLOPT_POSTFIELDS, 
//          http_build_query(array('postvar1' => 'value1')));

// receive server response ...
curl_setopt($xh, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($xh);
if (curl_errno($xh)) {
   echo 'Error:' . curl_error($xh);
}
curl_close ($xh);

// further processing ....
?>

检查一下您的评论......为什么它无效

    <?php
       $datacamp = array('foo'=>'bar','baz'=>'boom','cow'=>'milk','php'=>'hypertext processor');
       echo "<pre>";print_r($datacamp);
       echo http_build_query(array($datacamp['foo']=>0), '', '&amp;');
    ?>