简体   繁体   English

GET方法的PHP cURL函数

[英]PHP cURL function for GET method

I am trying to write a cURL function for GET method. 我正在尝试为GET方法编写一个cURL函数。 Following is the code: 以下是代码:

    $ch = curl_init();
    $token="43e6c623dda8f35dXXXXec57d58e91154a";
    $code="13200XX";
    $url="https://test.delhivery.com/c/api/pin-codes/json/?token=".$token."&filter_codes=".$code;
    echo $url;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPGET, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $return = curl_exec ($ch);
    curl_close ($ch);
    echo $return;

When I echo the above $url , I am getting the desired output but where I am wrong to write a cURL function, because when I am placing the same URL token in the cURL function I am getting blank page. 当我赞同上述$url ,我得到了想要的输出,但在那里我错写的卷曲功能,因为当我把相同的URL令牌卷曲功能我得到的空白页。

Please help me. 请帮我。

There may be plenty of reasons why it is not working. 可能有很多原因导致它无法正常工作。

Before doing all these steps below check if url in browser or postman or any other client returns correct response and also set error_reporting(E_ALL) and ini_set('display_errors', 1) 在执行以下所有步骤之前,请检查浏览器或邮递员或任何其他客户端中的url是否返回正确的响应,并设置error_reporting(E_ALL)ini_set('display_errors', 1)

  1. Check what's the output of echo curl_error($ch); 检查echo curl_error($ch);的输出是什么echo curl_error($ch); if it's empty then go to next steps 如果为空,则转到下一步
  2. Add browser header with curl_setopt() CURLOPT_USERAGENT some servers maybe set to deny connection if there is no browser header 使用curl_setopt() CURLOPT_USERAGENT添加浏览器标头,如果没有浏览器标头,则某些服务器可能设置为拒绝连接
  3. Accept cookies in curl with curl_setop( ) CURLOPT_COOKIEFILE 使用curl_setop(CURLOPT_COOKIEFILE接受curl中的cookie
  4. This url is secure that is why you need either to add certifcate or set CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 此网址是安全的,这就是为什么您需要添加证书或将CURLOPT_SSL_VERIFYPEERCURLOPT_SSL_VERIFYHOST选项设置为0的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM