Swikrit
Swikrit

Reputation: 351

cURL not working in PHP?

I have the following php function:

            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. As soon as I insert a proper JQL query, it fails. When I enter random garbage, it works (As in I get a invalid input message), but when it's proper JQL it never works.

When I copy and paste the URL that I echo into the browser it works. An example filter I used:

"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). But I'm really curious now. Anyone have ideas on what it might be?

Upvotes: 0

Views: 158

Answers (1)

Ioannis Lalopoulos
Ioannis Lalopoulos

Reputation: 1511

You should URL-encode the $filter.

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.

The reason why the URL works in the browser but not in the script is because the browser does the URL encoding.

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

Upvotes: 1

Related Questions