[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

cURL no response

Posted on 2014-08-18
35
Medium Priority
?
1,324 Views
Last Modified: 2014-08-18
Hello I am working with a simple API.  I am not sure what I am doing wrong. When I put the URL into the browser and any other restful API tester it seems to work fine..I have the code I am using below, It is just test credentials.  If you put it in the browser it works when I run cUrl it fails..Any help would be appreciated..Thanks!


$ch = curl_init('http://66.37.155.94:8080/Lsr/GetReports?clientId=awDemo&accessKey=weatherOrNot&lat=39.3178853&lon=-83.0144546&startTime=2004-08-18&stopTime=2014-08-18&type=Hail&type=Tornado&radius=20&responseFormat=json');
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                   
'Content-Type: text/plain; charset=utf-8 ',
'Content-Length: 4198',
'Accept: */*',
'Accept-Encoding: gzip,deflate,sdch',
'Accept-Language: en-US,en;q=0.8,sr;q=0.6,cs;q=0.4'                                                                                
    )                                                                       
);                                                                                                                   
$result = curl_exec($ch);
var_dump($result);
/////////////////////////CONVERT THE RESULTS INTO AN ARRAY//////////////////////////////
$assocArray = json_decode($result, true);

Open in new window

0
Comment
Question by:bradley525
  • 15
  • 14
  • 4
  • +1
34 Comments
 
LVL 12

Expert Comment

by:Radek Baranowski
ID: 40267639
what exact result you are getting ?

I ran your code on my local Xampp server and it runs fine, I get full string in response to curl call
0
 

Author Comment

by:bradley525
ID: 40267640
Hmmm mine just keeps thinking and nothing happens...
0
 

Author Comment

by:bradley525
ID: 40267649
The same string as if you but the url in the browser address bar?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Expert Comment

by:Radek Baranowski
ID: 40267662
Didn't understand the above..

I put the code exactly as you pasted it in your first post,

$ch = curl_init('http://66.37.155.94:8080/Lsr/GetReports?clientId=awDemo&accessKey=weatherOrNot&lat=39.3178853&lon=-83.0144546&startTime=2004-08-18&stopTime=2014-08-18&type=Hail&type=Tornado&radius=20&responseFormat=json');
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                   
'Content-Type: text/plain; charset=utf-8 ',
'Content-Length: 4198',
'Accept: */*',
'Accept-Encoding: gzip,deflate,sdch',
'Accept-Language: en-US,en;q=0.8,sr;q=0.6,cs;q=0.4'                                                                                
    )                                                                       
);        
                                                                                                           
$result = curl_exec($ch);
echo "<br>displaying result:<br>";
var_dump($result);

Open in new window


 and in return I get this:

result ss
0
 

Author Comment

by:bradley525
ID: 40267704
I keep getting the follwing error report from CURL


CURL FAIL: http://66.37.155.94:8080/Lsr/GetReports?clientId=awDemo&accessKey=weatherOrNot&lat=39.3178853&lon=-83.0144546&startTime=2004-08-18&stopTime=2014-08-18&type=Hail&type=Tornado&radius=20&responseFormat=json TIMEOUT=5, CURL_ERRNO=7array(22) { ["url"]=> string(203) "http://66.37.155.94:8080/Lsr/GetReports?clientId=awDemo&accessKey=weatherOrNot&lat=39.3178853&lon=-83.0144546&startTime=2004-08-18&stopTime=2014-08-18&type=Hail&type=Tornado&radius=20&responseFormat=json" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(5.000333) ["namelookup_time"]=> float(0.00014) ["connect_time"]=> float(0) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } ["redirect_url"]=> string(0) "" }
0
 
LVL 12

Expert Comment

by:Radek Baranowski
ID: 40267715
your machine has no route to the server you try to reach:

[7] => 'CURLE_COULDNT_CONNECT',

http://php.net/manual/en/function.curl-errno.php

can you do

ping 66.37.155.94

and

telnet 66.37.155.94 8080

?
0
 

Author Comment

by:bradley525
ID: 40267723
This is the error I get

Curl error: Couldn't resolve host '404.php.net'
0
 

Author Comment

by:bradley525
ID: 40267729
I can not ping 66.37.155.94 either
0
 
LVL 12

Expert Comment

by:Radek Baranowski
ID: 40267733
sorry, when you are getting this error ?

The url I gave above is a manual reference, not anything you should put in your code.

Can you please run two above commands (ping and telnet) from command line and share the results ?
0
 
LVL 12

Expert Comment

by:Radek Baranowski
ID: 40267747
so this must be the reason why curl is not working. What machine you are running your php/apache on ?

Is it the same machine on which you can reach the desired url from browser ? If yes, then your browser must have some kind of proxy that routes the request to the server, whereas your php server has it blocked somehow.

If you can't ping and/or telnet the machine you are trying to reach in curl, it is the root cause - no connection to the server.

fix that, and your curl will start working
0
 

Author Comment

by:bradley525
ID: 40267748
When I telnet the command line window is just black
0
 
LVL 12

Expert Comment

by:Radek Baranowski
ID: 40267753
yes, that means you have no network route to the server you are trying to connect to. work on that first.
0
 

Author Comment

by:bradley525
ID: 40267756
What does that mean? no network route?
0
 
LVL 12

Expert Comment

by:Radek Baranowski
ID: 40267761
It means you can't connect to this server - network path/route is blocked by firewall or other kind of network problem.
0
 

Author Comment

by:bradley525
ID: 40267764
AH, I tried it on a different server and it still seems to be blocked...
0
 
LVL 12

Expert Comment

by:Radek Baranowski
ID: 40267768
I'm not aware of your network setup and infrastructure, If you in some kind of company network, you need to work with network admins to open network path to the ip you want. I can't help you much with that :)
0
 

Author Comment

by:bradley525
ID: 40267775
I am in my own office..I am actually just trying to run it from my cell phone and it still fails.
0
 

Author Comment

by:bradley525
ID: 40267781
I am going to get with this company and see what the issue is...I will update you once I resolve it..

Thank you
0
 
LVL 12

Expert Comment

by:Radek Baranowski
ID: 40267786
ok, wait.

this server does not seem to respond to pings.

can you post me a screen shot of what happens when you do

"telnet 66.37.155.94 8080" ?
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 664 total points
ID: 40267796
Please see http://iconoun.com/demo/temp_bradley525.php -- it worked fine for me on my server.

You may not be successfully connected to the internet?

Here is the code I used:
<?php // /demo/temp_bradley525.php
error_reporting(E_ALL);

// SEE: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28499811.html
$url = 'http://66.37.155.94:8080/Lsr/GetReports?clientId=awDemo&accessKey=weatherOrNot&lat=39.3178853&lon=-83.0144546&startTime=2004-08-18&stopTime=2014-08-18&type=Hail&type=Tornado&radius=20&responseFormat=json';

// TRY THE REMOTE WEB SERVICE
$htm = my_curl($url);

// SHOW THE WORK PRODUCT OR BARK OUT ERROR MESSAGES
echo "<pre>";
$obj = json_decode($htm);
print_r($obj);


// A FUNCTION TO RUN A CURL-GET CLIENT CALL TO A FOREIGN SERVER
function my_curl
( $url
, $timeout=5
, $error_report=TRUE
)
{
    $curl = curl_init();

    // HEADERS AND OPTIONS APPEAR TO BE A CHROME BROWSER REFERRED BY GOOGLE
    $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: "; // BROWSERS USUALLY LEAVE THIS BLANK

    // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
    curl_setopt( $curl, CURLOPT_URL,            $url  );
    curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36'  );
    curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header  );
    curl_setopt( $curl, CURLOPT_REFERER,        'http://www.google.com'  );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );
    curl_setopt( $curl, CURLOPT_AUTOREFERER,    TRUE  );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE  );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout  );
    curl_setopt( $curl, CURLOPT_VERBOSE,        TRUE   );
    curl_setopt( $curl, CURLOPT_FAILONERROR,    TRUE   );


    // IF USING SSL, THESE MAY BE IMPORTANT
    curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, FALSE  );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE  );
    curl_setopt( $curl, CURLOPT_SSLVERSION,     3      );

    // RUN THE CURL REQUEST AND GET THE RESULTS
    $htm = curl_exec($curl);

    // ON FAILURE
    if ($htm === FALSE)
    {
        // VISUALIZE ERROR MESSAGES
        if ($error_report)
        {
            $err = curl_errno($curl);
            $inf = curl_getinfo($curl);
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            var_dump($inf);
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS RETURN XML / HTML STRING
    curl_close($curl);
    return $htm;
}

Open in new window

0
 

Author Comment

by:bradley525
ID: 40267837
This is what I am getting


CURL FAIL: http://66.37.155.94:8080/Lsr/GetReports?clientId=awDemo&accessKey=weatherOrNot&lat=39.3178853&lon=-83.0144546&startTime=2004-08-18&stopTime=2014-08-18&type=Hail&type=Tornado&radius=20&responseFormat=json TIMEOUT=5, CURL_ERRNO=28array(22) { ["url"]=> string(203) "http://66.37.155.94:8080/Lsr/GetReports?clientId=awDemo&accessKey=weatherOrNot&lat=39.3178853&lon=-83.0144546&startTime=2004-08-18&stopTime=2014-08-18&type=Hail&type=Tornado&radius=20&responseFormat=json" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(5.001577) ["namelookup_time"]=> float(0.001204) ["connect_time"]=> float(0) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } ["redirect_url"]=> string(0) "" }
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 40267851
Sounds more like a network connection issue.

1. Is your browser configured to use a proxy? If so, have you told cURL to use that same proxy?

2. Are you using any client-side security software (e.g. McAfee HIPS or a firewall of some kind) ?
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 40267863
Sorry, I just saw Radek's comments about the proxy and network - disregard my duplicate comments.
0
 

Author Comment

by:bradley525
ID: 40267881
I have put this code on 3 different servers and I get the same error each time I try to run it



CURL FAIL: http://66.37.155.94:8080/Lsr/GetReports?clientId=awDemo&accessKey=weatherOrNot&lat=39.3178853&lon=-83.0144546&startTime=2004-08-18&stopTime=2014-08-18&type=Hail&type=Tornado&radius=20&responseFormat=json TIMEOUT=5, CURL_ERRNO=7array(20) { ["url"]=> string(203) "http://66.37.155.94:8080/Lsr/GetReports?clientId=awDemo&accessKey=weatherOrNot&lat=39.3178853&lon=-83.0144546&startTime=2004-08-18&stopTime=2014-08-18&type=Hail&type=Tornado&radius=20&responseFormat=json" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0) ["namelookup_time"]=> float(0.006058) ["connect_time"]=> float(0) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) }
0
 
LVL 12

Expert Comment

by:Radek Baranowski
ID: 40267895
are those some standalone servers ? Linux or windows ?

Can you launch browser on either of them (be sure that you run browser on the machine you are having problem on, not your local machine from which you have internet access eg. to this site) and try to put http://66.37.155.94:8080/Lsr/GetReports?clientId=awDemo&accessKey=weatherOrNot&lat=39.3178853&lon=-83.0144546&startTime=2004-08-18&stopTime=2014-08-18&type=Hail&type=Tornado&radius=20&responseFormat=json in address bar and tell us what happens ?
0
 

Author Comment

by:bradley525
ID: 40267903
it is a standalone server and when i click on the URL all of the data shows up that I am trying to pull in via cURL
0
 
LVL 12

Expert Comment

by:Radek Baranowski
ID: 40267932
and your apache/php server is running on the very same machine you are
"clicking on the URL all of the data shows up that I am trying to pull in via cURL" ??

can you change your code to

$ch = curl_init('http://google.com');

Open in new window


and see what happens ?

what are you running your php server on (I mean, what are you typing in the browser to reach your php file to run?
have you ever been able to use curl from within your php work ? if this is first time, maybe your php server (like Xampp in my case) is blocked by local machine firewall (like windows firewall or symantec endpoint protection) and you can reach external addresses from the browser, but not from other applications (not the ones added to firewall exceptions list).

eg. I'm running local xampp so in chrome I'm typing in:

localhost/testfile.php

frankly, I'm having hard time understanding your setup :(
0
 

Author Comment

by:bradley525
ID: 40267952
I do not receive any error when I use the google url...


I am running this on a VPS. The server is not local, it is running CENTOS and a LAMP stack...
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 40267981
Is your remote server configured with portables? If so, are you logging rejected packets?
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 40267984
Sorry - auto correct screwed up my comment. "iptables" not "portables"
0
 
LVL 12

Accepted Solution

by:
Radek Baranowski earned 1336 total points
ID: 40268030
ok so you are testing the url in your local browser and it works

then you try the code from the remote server it it doesn't, but it works with google.com, am I right ?

if so, then try to run telnet 66.37.155.94 8080 on your remote server and see if it skips to blank screen (that means you got connection, and you can terminate it with Ctrl+]). if not, then you need to work with your VPS provider (is it a kind of virtualization provider or hosting provider ?) to open network path to  66.37.155.94 from there. or, as Gr8gonzo suggest - check iptables there (go to shell and run iptables -L and iptables -L -t NAT ) and post it here.
0
 

Author Comment

by:bradley525
ID: 40268103
Looks like my servers firewall was blocking it this whole time...You guys have been alot of help. I am not really sure how to award points on this one but I will do my best since it was really an issue on my side.

Thanks!
0
 
LVL 12

Assisted Solution

by:Radek Baranowski
Radek Baranowski earned 1336 total points
ID: 40268105
Networking for sure
0
 
LVL 12

Expert Comment

by:Radek Baranowski
ID: 40268123
ok glad you solved it. thx for the points.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question