使用cURL和php

时间:2016-06-30 22:56:21

标签: php curl shopify

我有一个cURL从URL响应中获取响应,其格式如下:

{"status": "OK", "tracking_id": "363070a8-53b6-49da-a7e9-6a289a3501f2"}

有时cURL会执行多次,当我只执行一次时,我会收到1到5个不同的响应。

我执行的URL看起来像这样:

    $request =  "https://precise-line.com/2/delivery/request?";
                $request.=  "api_key=".$api_key."&";
                $request.=  "user_id=".$user_id."&";
                $request.=  "delivery_type=".$delivery_type."&";
                $request.=  "route=E.+Pallares+y+Portillo&";
                $request.=  "street_number=110&";
                $request.=  "neighborhood=San+Lucas&";
                $request.=  "locality=Distrito+Federal&";
                $request.=  "administrative_area_level_1=Distrito+Federal&";
                $request.=  "postal_code=04030&";
                $request.=  "country=Mexico&";
                $request.=  "latlng=".$latlng."&";
                $request.=  "destination-route=".$destination_route."&";
                $request.=  "destination-street_number=&";
                $request.=  "destination-neighborhood=&";
                $request.=  "destination-locality=".$destination_locality."&";
                $request.=  "destination-administrative_area_level=".$destination_administrative_area_level."&";
                $request.=  "destination-postal_code=".$destination_postal_code."&";
                $request.=  "destination-country=Mexico&";
                $request.=  "destination-latlng=".$d_latlng."&";
                $request.=  "customer_email=".$customer_email."&";
                $request.=  "customer_phone=".$customer_phone."&";
                $request.=  "notification_email=&";
                $request.=  "notes=".$notes."&";
                $request.=  "dispatch=True";

 error_log("Request");
            error_log(print_r($request, true));

            $ch_request=curl_init();
            $curl =  curl_init();
            curl_setopt($curl, CURLOPT_URL, $request);
            curl_setopt($curl, CURLOPT_SSLVERSION, 1);
            //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_HEADER, FALSE);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

            $response = curl_exec($curl);
            curl_close ($curl);

            error_log("Response");
            error_log(print_r($response, TRUE));

如何防止cURL执行多次?

1 个答案:

答案 0 :(得分:0)

你为什么同时拥有$ ch_request和$ curl?我不知道这是否有所不同,但您应该使用$ ch_request删除该行。否则,正如Dagon所说,如果你打电话一次,你不应该有多个回复。