1

I'm trying to query to an OData web service. I tried doing this in Python and it worked fine. But when I tried to do it in PHP, I get the following error:

Curl error: Failed to connect to fse-na-int01.cloud.clicksoftware.com port 443: Connection refused

This is what I tried so far in PHP:

function getTaskStatus($taskKey){
    $curl = curl_init();
    $url = "https://fse-na-int01.cloud.clicksoftware.com/so/api/objects/Task/?\$select=DisplayStatus&\$filter= Key eq ".$taskKey;


    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE);
    curl_setopt($curl, CURLOPT_HTTPHEADER, [
        'Accept: application/json',
        'Content-Type: application/json'
    ]);
    curl_setopt($curl, CURLOPT_USERPWD, "user@domain:password");
    $result = curl_exec($curl);
    if(curl_errno($curl)){
        echo 'Curl error: ' . curl_error($curl);
    }
    curl_close($curl);
    return $result[0]["DisplayStatus"]["@DisplayString"];

}

And this is my Python code (which works fine):

def getTaskStatus(taskKey):
        top_level_url = "https://fse-na-int01.cloud.clicksoftware.com/"
        usuarioClick = 'user@domain'
        passwordClick = 'password'
        try: 
            password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
            password_mgr.add_password(None, top_level_url, usuarioClick, passwordClick)
            handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
        # create "opener" (OpenerDirector instance)
            opener = urllib.request.build_opener(handler)
        # use the opener to fetch a URL
            a_url = "https://fse-na-int01.cloud.clicksoftware.com/so/api/objects/Task/?$select=DisplayStatus&$filter=%20Key%20eq%20"+str(taskKey)
            opener.open(a_url)
            urllib.request.install_opener(opener)
            response = urllib.request.urlopen(a_url)
            resultadoJson = json.loads(response.read())

        except urllib.error.HTTPError:
            return "AuthError"
        except urllib.error.URLError:
            return "ConnError"
        except:
            return "UnkownError"
        try:
            return resultadoJson[0]["DisplayStatus"]["@DisplayString"]
        except:
            return False

Any ideas why the PHP code isn't working?

Thanks!

NGabioud
  • 357
  • 3
  • 20
  • Try to add `curl_setopt($curl, CURLOPT_PORT, 443);` – Zhi V Jan 21 '20 at 15:14
  • Thank you @ZhiV , but it's still not working. Could it be something related to @ in the user? – NGabioud Jan 21 '20 at 15:16
  • Try to follow suggestions in this question: https://stackoverflow.com/questions/35512290/how-to-send-odata-to-restful-api-in-php-curl-request – SergeyAn Jan 21 '20 at 15:17
  • @SergeyAn thanks. I've already tried that, but i was still getting the connection refused – NGabioud Jan 21 '20 at 15:22
  • Anyway you need to somehow ensure that your url is being built properly (try to break your url in your python implementation to see possible causes) and if it does then try to follow this debugging info: https://stackoverflow.com/questions/3757071/php-debugging-curl – SergeyAn Jan 21 '20 at 15:41
  • The url is fine. It works in Python and when I paste it in chrome, I get the info I need. The problem seems to be during authentication @SergeyAn – NGabioud Jan 21 '20 at 15:51
  • If you use proxy it may also cause issues. If you don't then try to remove CURLOPT_HTTPHEADER and check, if doesn't work remove CURLAUTH_ANYSAFE and check, and then CURLOPT_RETURNTRANSFER. Basically try from minimum settings to maximum. – SergeyAn Jan 21 '20 at 16:04
  • Thanks @SergeyAn , I tried only leaving CURLOPT_USERPWD and CURLOPT_URL, but it's till not working. Do you know if it could be something related to "@" in the user? – NGabioud Jan 21 '20 at 16:21
  • Not really, but try also to change the value of CURLOPT_HTTPAUTH to CURLAUTH_ANY. One of these might be relevant https://stackoverflow.com/questions/23809392/what-does-phps-curlopt-userpwd-do or https://stackoverflow.com/questions/4753648/problems-with-username-or-pass-with-colon-when-setting-curlopt-userpwd – SergeyAn Jan 21 '20 at 16:42
  • Thanks @SergeyAn, i tried both but it's still not working :( – NGabioud Jan 21 '20 at 17:06
  • I tried accesing with Advance REST Client Chrome app. And i'm being able to get it. For some reason PHP is not working. – NGabioud Jan 22 '20 at 16:35
  • I also tried the hash created in ARC for the authorization. But i'm still getting "Connection Refused". The line I added was: curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authentication: Basic aW50ZWdyYXR......................Y29t")); /*added some dots to not paste the whole hash */ – NGabioud Jan 22 '20 at 16:41
  • I don't know if it works. But i added curl_getinfo and I'm getting error "Undefined index: request_header" even though I added CURLINFO_HEADER_OUT @SergeyAn . Could it be that the options are not setting correctly? – NGabioud Jan 22 '20 at 16:55
  • 1
    Of course, it can be. That is way I also said that you need to make sure that your url is built properly. If it does then you can check other parameters. https://stackoverflow.com/questions/35512290/how-to-send-odata-to-restful-api-in-php-curl-request in this link I mentioned the answer clearly states that improper url may not work. You need to eliminate step-by-step things that work and come to the things that don't work. That's how debugging works. I can only guess what is not working. Or if you want , you can provide me temporary access to check. And I will check. – SergeyAn Jan 23 '20 at 07:53

0 Answers0