0

I have a problem in curl for one specific server. I have a service api and its working fine from other servers but on this specific one i am having issue. Code i tried is as below :

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

$url = 'http://services.exportfeed.com/test.php';
$postfields = array(
    'fetch'   => '1',
    'country' => 'US',
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    $error_message = 'Curl error: ' . curl_error($ch);
    print_r($error_message);exit;
}
curl_close($ch);

$result = json_decode($result);
echo "<pre>";
var_dump($result);
echo "</pre>";
exit;

While hitting the url it just returns null, no error no warning nothing. When i curl from terminal it returns the message it should return.

Can anyone help me solving this issue. I followed this and this guide but this did not solve. My hosting said like this reply from host

I am stuck in this. Please help.

10
  • 1
    What does curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 2); do? Is 2 a valid value there? json_decode($var) can produce null if you don't pass it valid json. I would check the output before attempting to decode. Commented Aug 9, 2018 at 19:07
  • 1
    why curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 2); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); if you hav just http url ? Commented Aug 9, 2018 at 19:08
  • I tried your code, it worked perfectly. Commented Aug 9, 2018 at 19:42
  • CURLOPT_SSL_VERIFYPEER should be a boolean (true or false). Setting to 2 is meaningless (it is meaningful to CURLOPT_SSL_VERIFYHOST however). Commented Aug 9, 2018 at 19:51
  • var dump the result before passing it through json_encode. Json encode only supports utf8 chars and will return false if it fails Commented Aug 9, 2018 at 20:40

0

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.