【问题标题】:error posting data with curl and ssl使用 curl 和 ssl 发布数据时出错
【发布时间】:2016-05-18 19:22:52
【问题描述】:

我用这个 curl 发布数据:

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

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

但是我检查了数据没有发布,我怎么知道?因为我把它写在一个error_log中,如果数据已经发布,系统会响应OK,但是当我检查我的error_log时我什么也没有收到。

我尝试发布的 URL 是自动生成的,类似于:

https://precise-line.com/request?api_key=23894thfpoiq10f&user_id=508958644&delivery_type=Programado&route=Eje+1+norte&street_number=40&neighborhood=Centro&locality=Mexico&administrative_area_level_1=Distrito+Federal&postal_code=00650&country=Mexico&latlng=19.3596892%2C-99.2788723&destination-route=Calle+pinco+85%2C+Int.+1%2C+Col.+Florida%2C+Del.+%C3%81lvaro+Obreg%C3%B3n&destination-street_number=&destination-neighborhood=&destination-locality=Ciudad+de+M%C3%A9xico&destination-administrative_area_level=Distrito+Federal&destination-postal_code=01080&destination-country=Mexico&destination-latlng=19.3627808%2C-99.1757137&customer_email=ink_design%40hotmail.com&customer_phone=56581111&notification_email=&notes=Orden%3A+%2316652%2C+Cliente%3A+Said+Pe%C3%B1a+Amezcua%2C+Productos%3A+DEVIL+MAY+CRY+DEFINITIVE+EDITION+1%2C+&dispatch=True

这就是我生成网址的方式:

$api_key=                                   '23894thfpoiq10f';
            $user_id=                                   '508958644';
            if ($shipping == $program){
                $delivery_type =    'Programado';
            }
            if ($shipping == $express){
                $delivery_type =    '99minutos';
            }
            $latlng =                                   '19.4165176%2C-99.1544438';
            $customer_email=                            urlencode($email);
            $destination_route=                         urlencode(implode(',' , array($address1,$address2)));
            $destination_locality=                      urlencode($city);
            $destination_administrative_area_level=     urlencode($province);
            $destination_postal_code=                   urlencode($zip);
            $d_latlng=                                  urlencode(implode(',', array($latitude,$longitude)));
            $customer_phone=                            urlencode($phone);
            $nombre =                                   'Cliente: '.implode(' ',array($first_name,$last_name));
            $orden =                                    'Orden: '.$name;
$request =  "https://precise-line.com/request?";
            $request.=  "api_key=".$api_key."&";
            $request.=  "user_id=".$user_id."&";
            $request.=  "delivery_type=".$delivery_type."&";
            $request.=  "route=Av+Cuauhtemoc&";"street_number=187&";
            $request.=  "neighborhood=Roma+Norte&";
            $request.=  "locality=Mexico&";
            $request.=  "administrative_area_level_1=Distrito+Federal&";
            $request.=  "postal_code=06700&";
            $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";

【问题讨论】:

  • 为什么不使用 Postman 来测试您使用的 API。您将知道您提出的请求是否正确以及您得到的响应是什么。在您的情况下,我认为您应该执行 GET 请求,因为您的 URL 包含所有数据。
  • 是的,如果我使用 Postman 测试 URL 数据是发布的,但没有它我没有响应!
  • 因此,如果您使用 Postman 得到正确响应,请单击生成代码并查看您缺少哪些 curl 选项。这会有所帮助。

标签: php ssl curl


【解决方案1】:

你需要为 curl 添加另外 2 个选项:

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

$params 应该是您想要的参数的关联数组,例如:

$params = array('api_key'=>'abcde')

另外,curl_setopt($curl, CURLOPT_URL, $request); 中的$request 的值应该是没有https://precise-line/request 等参数的基本网址

【讨论】:

  • 但前几天我发布的 Curl 有效,但从上周五到现在 cURL 无效
  • 如果此解决方案解决了您的问题,请接受答案
  • 对不起,但我看到解决方案在其他东西中......它在 ssl 版本中是 1 而不是 3,就像我的例子一样!
【解决方案2】:

我找到了解决方案,它是在SSL版本中,我写了3,解决我的问题的版本是1,但是感谢所有帮助我的人

【讨论】:

    猜你喜欢
    • 2014-09-15
    • 2015-03-23
    • 2013-02-18
    • 2012-12-08
    • 2014-01-22
    • 1970-01-01
    • 2015-06-03
    • 2015-09-27
    • 1970-01-01
    相关资源
    最近更新 更多