cURL on own URL not working. unable to get any response

I am developing a WordPress plugin for my site. In the Plugin, there is a piece of code that is supposed to hit a service that is again hosted on my own site.

the reason for this self call is that the plugin should be later on redistributed to other WordPress sites owned by me…

Read More

The service is perfectly working when the service is deployed on the production website and is tested from the plugin installed in the local dummy installation of WordPress site.

when the same plugin is installed in the Production site, the response comes as null. Unable to get any kind of response on the same.

I am using the below code to call the REST Service. Method is POST.

function callAPI($method, $url,$headers, $data = false)
{
    $curl = curl_init();

    switch ($method)
    {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);
            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_PUT, 1);
            break;
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }
    // Optional Authentication:
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
}

As per expectation, if I call the URL and just echo out some dump data, then also the data does not show up as response.

I am able to test the service from the Google Chrome extension for REST Testing as well

Related posts

Leave a Reply

1 comment

  1. As you said you can test the same call with a Google Chrome extension and it works fine, most likely something goes wrong with your cURL calls. You should check if $result is FALSE or not, and if it is then use curl_error for more information on what happened.

    Update:

    From the documentation:

    To post a file, prepend a filename with @ and use the full path.

    You have to escape the @ sign and it’ll work.