0

URL Working fine in Browser after accepting the exception , but not working in curl.

My code is as follows :::

PHP CODE :

     <?php


        $str1 = "https://50.60.70.80/servlet/API";

        $url = $str1."?".$_SERVER['QUERY_STRING'];

        echo $url;


        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL,$url);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST, true);


        $data = curl_exec($ch);

        echo curl_errno($ch);

        if (curl_errno($ch)) {
          echo 'Error: ' . curl_error($ch);
        }

        else {
                echo 'Success: '.$data;
        }

        curl_close($ch);


        ?>





    URL : https://50.60.70.80/servlet/API?userName=Test&password=pwd&message=My_Message&mobile=5555558888

Error getting while using through command line with "-k" and "-v":

 ***** About to connect() to 50.60.70.80 port 443 (#0)
*   Trying 50.60.70.80... connected
* Connected to 50.60.70.80 (50.60.70.80) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* warning: ignoring value of ssl.verifyhost
* skipping SSL peer certificate verification
* SSL connection using TLS_DHM_RSA_W1TH_AES_128_CBC_SEA
* Server certificate:
*       subject: [email protected],OU=lelna,O=teledna
*       start date: Mar 01 09:51:25 2013 GMT
*       expire date: Mar 01 09:51:25 2014 GMT
*       common name: (nil)
*       issuer: [email protected],OU=lelna,O=teledna
> GET /servlet/API?UserName=Test HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.3.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 50.60.70.80
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< Pragma: no-cache
< Cache-Control: no-store
< Expires: Thu, 01 Jan 1970 00:00:00 GMT
< Transfer-Encoding: chunked
< Date: Thu, 08 Aug 2013 16:38:57 GMT
<
status=FAILED,transid=4526698561,reasoncode=201
* Connection #0 to host 50.60.70.80 left intact
* Closing connection #0****

Can anybody help ?

I am using following versions :: PHP Version -- 5.3.3 CURL Version -- 7.19.7 Using Apache

4
  • not working how? returns bad data? crashes? Commented Aug 8, 2013 at 16:50
  • returns no data.. Actually calling the php file from my Application server/Application Browser .. If from browser browser times out. If from application not getting success code. tried doing the same from command line window by writing curl -k -v --url >> and then the url.. The error which i got is shown in the above snippet. Commented Aug 8, 2013 at 17:12
  • Should you be URL encoding your URL? Perhaps that may help, in case of dodgy characters being passed? Commented Aug 12, 2013 at 9:11
  • tried by passing curl_setopt($ch, CURLOPT_URL,urlencode($url)); Getting error could not resolve hostname Commented Aug 12, 2013 at 9:35

1 Answer 1

1

You need to add this

EDITED :

curl_setopt( $ch ,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
Sign up to request clarification or add additional context in comments.

5 Comments

Tried with all these : $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,urlencode($str1)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_HTTPGET, 1); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1); curl_setopt($ch, CURLOPT_HEADER ,0); // DO NOT RETURN HTTP HEADERS curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false ); curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 2 ); curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
@KmrGtm, why are you using HTTGET,1 and POST,1 in the same code ? How would that be supposed to work ?
@ Shankar I tried them in different scripts one by one .. getiing same result.. tried by passing curl_setopt($ch, CURLOPT_URL,urlencode($url)); Getting error could not resolve hostname –
@KmrGtm, Nope don't do urlencode. Seems like you are sending data via GET, so try my edited answer above. More importantly, don't remove the RETURNTRANSFER parameter , it is required.
Thanks @ Sankar .. Actually i figured out some more errors .Original PHP Code , which i tried earlier is also correct and working fine .. But the Real prob is : My php file is in Webserver. If iam calling the php file from the webserver itself using this command " php url.php " , its running fine . But in the real scenario , i want to call the php file from a Application server . That is what is creating problem. Any recommendation ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.