【问题标题】:cURL not working in PHP?cURL 在 PHP 中不起作用?
【发布时间】:2016-05-25 21:17:33
【问题描述】:

我有以下php函数:

            function checkJQL($filter) {
            $auth = "account:password";
            $URL='http://jira/rest/api/latest/search?jql='.$filter;
            echo $URL;

            //  Initiate curl
            $ch = curl_init();
            // Disable SSL verification
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            //Tell it to always go to the link
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            // Will return the response, if false it print the response
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            // Set the url
            curl_setopt($ch, CURLOPT_URL,$URL);
            //Set username and password
            curl_setopt($ch, CURLOPT_USERPWD, $auth);
            // Execute
            $result=curl_exec($ch);
            // Closing
            curl_close($ch);

            echo $result;
            // Will dump a beauty json :3
            var_dump(json_decode($result, true));
            echo "DONE";
        }

当我用$filter = "" 调用它时,它工作正常,输出所有内容。只要我插入正确的 JQL 查询,它就会失败。当我输入随机垃圾时,它可以工作(就像我收到无效的输入消息一样),但是当它是正确的 JQL 时它永远不会工作。

当我将回显的 URL 复制并粘贴到浏览器中时,它可以工作。 我使用的示例过滤器:

“目标”=“废话”

当我想到它时,我实际上并不需要它来工作,我只需要它知道输入何时不是 JQL(它确实如此)。但我现在真的很好奇。有人对它可能是什么有想法吗?

【问题讨论】:

    标签: php curl jira jira-rest-api


    【解决方案1】:

    您应该对 $filter 进行 URL 编码。

    它适用于空字符串或随机字符串的原因是因为它们没有任何需要进行 URL 编码的具有挑战性的字符。

    URL在浏览器中有效而在脚本中无效的原因是浏览器进行了URL编码。

    请执行以下操作:

    $URL='http://jira/rest/api/latest/search?jql='.urlencode($filter);
    

    网址链接:http://php.net/manual/en/function.urlencode.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 2015-07-08
      • 1970-01-01
      相关资源
      最近更新 更多