2
$url='http://api.cruiseline.com/cruises/7-night-western-caribbean-ft-lauderdale-roundtrip-35052/detail';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$result=json_decode($result);
var_dump($result);

Here is the response of curl_getinfo()

array(26) { ["url"]=> string(96) "http://api.cruiseline.com/cruises/7-night-western-caribbean-ft-lauderdale-roundtrip-35052/detail" ["content_type"]=> string(16) "application/json" ["http_code"]=> int(200) ["header_size"]=> int(511) ["request_size"]=> int(127) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(5.11) ["namelookup_time"]=> float(0.203) ["connect_time"]=> float(0.532) ["pretransfer_time"]=> float(0.532) ["size_upload"]=> float(0) ["size_download"]=> float(650744) ["speed_download"]=> float(127347) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(0) ["starttransfer_time"]=> float(1.125) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } ["primary_ip"]=> string(14) "75.101.141.196" ["primary_port"]=> int(80) ["local_ip"]=> string(13) "192.168.10.15" ["local_port"]=> int(59916) ["redirect_url"]=> string(0) "" } 

Above code sometimes sends complete json data and sometimes sends partial data to $result. When it sends incomplete data, json_decode returns null.

There is no issue on server side. When we open http://api.cruiseline.com/cruises/7-night-western-caribbean-ft-lauderdale-roundtrip-35052/detail in browser, it sends complete response all the time.

Please help and let me know what is the problem and how I can fix that.

6
  • 1
    Check out the curl_getinfo($ch) before closing it... And post it, might help us. Commented Jun 13, 2016 at 7:54
  • Try adjusting the timeouts Commented Jun 13, 2016 at 7:56
  • I just submitted the response from curl_getinfo() Commented Jun 13, 2016 at 8:24
  • @DarkBee curl_setopt($ch, CURLOPT_TIMEOUT, 400) and also used curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5) but no difference. Commented Jun 13, 2016 at 8:25
  • it is possible that this is caused due to non-utf8 characters? Try to utf8 encode your $result $result= utf8_encode($result); and then json_decode() it Commented Jun 13, 2016 at 8:35

1 Answer 1

6

Add this:

curl_setopt($ch, CURLOPT_ENCODING, '');

to your cURL setup. This made your code workable for me.

Sign up to request clarification or add additional context in comments.

5 Comments

It seems working now. Can you please explain how CURLOPT_ENCODING works?
I think, that there was something wrong with encoding of received data. Per manual: if an empty string, "", is set, a header containing all supported encoding types is sent. That's why now the data is decoded correctly.
Thanks for explanation. But for same data it was working some time and sometimes it was giving an error.
Cause that "some data" was encoded correctly. It is hard to predict, what data will be sent, that's why for such cases it is useful to enable automatic encoding.
Thank you so much.

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.