简体   繁体   English

cURL在PHP中不起作用?

[英]cURL not working in PHP?

I have the following php function: 我有以下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";
        }

When I call this with $filter = "" It works fine, outputs everything. 当我用$filter = ""调用它时,它工作正常,输出所有内容。 As soon as I insert a proper JQL query, it fails. 一旦插入正确的JQL查询,它就会失败。 When I enter random garbage, it works (As in I get a invalid input message), but when it's proper JQL it never works. 当我输入随机垃圾时,它可以工作(就像我收到无效的输入消息一样),但是当它是正确的JQL时,它就永远无法工作。

When I copy and paste the URL that I echo into the browser it works. 将复制的URL复制并粘贴到浏览器后,它将起作用。 An example filter I used: 我使用的示例过滤器:

"Target" = "Blah" “ Target” =“ Blah”

When I think about it, I don't actually need this to work, I just need it to know when the input isn't JQL (which it does). 当我考虑它时,我实际上并不需要它工作,我只需要它知道什么时候输入不是JQL(它确实如此)。 But I'm really curious now. 但是我现在真的很好奇。 Anyone have ideas on what it might be? 任何人都有可能的想法吗?

You should URL-encode the $filter . 您应该对$filter进行URL编码。

The reason why it works with empty or random strings is because they don't have any challenging characters that need to be URL-encoded. 之所以可以使用空字符串或随机字符串,是因为它们没有需要URL编码的任何具有挑战性的字符。

The reason why the URL works in the browser but not in the script is because the browser does the URL encoding. URL在浏览器中有效但在脚本中无效的原因是因为浏览器执行URL编码。

So do the following: 因此,请执行以下操作:

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

Link to urlencode: http://php.net/manual/en/function.urlencode.php 链接到urlencode: http : //php.net/manual/en/function.urlencode.php

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

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