使用PUT服务器请求卷曲到php

时间:2016-11-28 14:22:46

标签: php json curl

我可以使用GET显示值,但我不知道如何使用PUT提交将改变我的数据值的请求。 这是我的代码:

 if($visit >= 10){

                    $wh = curl_init();

                curl_setopt($wh, CURLOPT_URL, "https://core.voluum.com/campaigns/" . $id);
                curl_setopt($wh, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($wh, CURLOPT_CUSTOMREQUEST, "GET");


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

                $resulta = curl_exec($wh);
                if (curl_errno($wh)) {
                    echo 'Error:' . curl_error($wh);
                }
                echo "Request:" . "<br>";
                $campinfo = json_decode($resulta, true);
                echo '<pre>' . print_r($campinfo, TRUE) . '</pre>';
                foreach($campinfo['pathsGroups'] as $datacamp){
                    echo $datacamp['active'];

以下是我的数据的一部分:

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 // change this to false so that it will be turned off in in the website
                )

        )



)

我想更改活动,以便我可以关闭路径组

1 个答案:

答案 0 :(得分:0)

您需要确保带有PUT的网址执行更新(这是一项惯例而非义务)。 您需要完全了解数据的样子。

这里看起来应该是什么样的(有一些解释......)

            // New values
            // This should contain all the values with the new value of the active key
            // it should start like this based on the output you provided
            // $data = array(
            //     'pathsGroups' => array(
            //           array(
            //              'condition' => ...
            //    ...etc
            // );
            $data = array(
                 // values
            );

            $wh = curl_init();

            curl_setopt($wh, CURLOPT_URL, "https://core.voluum.com/campaigns/" . $id);
            curl_setopt($wh, CURLOPT_RETURNTRANSFER, 1);
            // You use put exactly the same way as GET
            curl_setopt($wh, CURLOPT_CUSTOMREQUEST, "PUT");
            // Here you pass your new values, with GET we don't have this
            curl_setopt($wh, CURLOPT_POSTFIELDS, http_build_query($data));

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

            $resulta = curl_exec($wh);

            if (curl_errno($wh)) {
                echo 'Error:' . curl_error($wh);
            }

            // Could help http://lornajane.net/posts/2009/putting-data-fields-with-php-curl