Can't connect to Wufoo API from PHP (localhost), but can connect using terminal curl OK

143 views Asked by At

If I run this in terminal (using my real api ID, of course) it works fine.

curl -u "MYAPIID":"footastic" "https://myaccountid.wufoo.com/api/v3/forms.json"

I'm trying to get the same result in PHP on my local server (localhost) using this:

$curl = curl_init('https://myaccountid.wufoo.com/api/v3/forms.json');

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Wufoo Form Getter');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_USERPWD, 'MYAPIID:footastic');

$response = curl_exec($curl);
$resultStatus = curl_getinfo($curl);
if ($resultStatus['http_code'] == 200) {
    $result = json_decode($response, true);
} else {
    $result = 'Call Failed ' . print_r($resultStatus, true);
}

echo "<pre>";
print_r($result);
echo "</pre>";

It always returns the 'Call Failed' result with the following data:

Call Failed Array
(
    [url] => https://myaccountid.wufoo.com/api/v3/forms.json
    [content_type] => 
    [http_code] => 0
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1
    [ssl_verify_result] => 1
    [redirect_count] => 0
    [total_time] => 0.216174
    [namelookup_time] => 4.1E-5
    [connect_time] => 0.215999
    [pretransfer_time] => 0
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 0
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 75.98.93.66
    [certinfo] => Array
        (
        )

    [primary_port] => 443
    [local_ip] => 192.168.0.9
    [local_port] => 54355
)

Why would it be working fine in Terminal but not in PHP? Any idea how to get this working?

1

There are 1 answers

0
Sabuj Hassan On

Remove curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); from your code. As per curl document:

CURLAUTH_ANY

This is a convenience macro that sets all bits and thus makes libcurl pick any it finds suitable. libcurl will automatically select the one it finds most secure.

If you use curl_setopt($curl, CURLOPT_VERBOSE, true); in your code then you'll see that it is not sending the basic authentication in header. But if you remove that line as I've mentioned, you'll see it is working.

You can also run your curl command using -v option. It will show you the debug prints. It will help you to match the request headers of your commandline and code.