Crazy Dino Crazy Dino - 20 days ago 5
PHP Question

PHP cURL options not being set

I have a php REST client that calls a local tomcat REST server and have been using this for the last couple of years on a production server.

I've had to make some unrelated client side CSS changes and have had to build my development environment to test that they're ok before pushing to prod. Due to some changes with the version of vagrant I have installed I've had to change the vagrant box it uses and I think this is what's causing all the issues.

It appears when I make a cURL request something weird is happening and it's not creating the request correctly.

This is my client code (which is currently working on production):

class RestClient {

private $baseEndpoint;
private $username;
private $password;
private $siteCode;

function __construct($baseEndpoint, $username, $password, $siteCode) {
$this -> baseEndpoint = $baseEndpoint;
$this -> username = $username;
$this -> password = $password;
$this -> siteCode = $siteCode;
}

public function get($endpoint) {
//die($this -> baseEndpoint . $endpoint); //comment1
$curlResource = curl_init($this -> baseEndpoint . $endpoint);
curl_setopt_array($curlResource, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPHEADER => array(
"Accept:application/json",
"Content-Type:application/json",
"Site-Code:" . $this -> siteCode,
),
CURLOPT_USERPWD => $this -> username . ":" . $this -> password
));

$curlResponse = curl_exec($curlResource);
// echo curl_error($curlResource); //comment2
$resultStatus = curl_getinfo($curlResource);
// print_r($resultStatus); die(); //comment3

$statusCode = $resultStatus['http_code'];

if($statusCode != 200) {
throw new Exception("500");
}

$restResponse = new Response();
$restResponse -> setStatus($statusCode);
$restResponse -> setEntity($curlResponse);

return $restResponse;
}



  • If I uncomment comment1 I get what I expect - "http://localhost:8080/webApp/endpoint"

  • If I uncomment comment2 I get "Failed to connect to ::1: Permission denied" which doesn't appear right at all... Where i've googled this error they all appear to show the request endpoint.

  • If I uncomment comment3 I get "Array ( [url] => http://localhost:8080/webApp/endpoint [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0 [namelookup_time] => 0.000515 [connect_time] => 0 [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] => 127.0.0.1 [certinfo] => Array ( ) ) " which seems to show none of my options are being set.



Somes notes:


  • When I run the curl request (curl --user user:password --header "site-code: london" http://localhost:8080/webApp/endpoint) I get the correct response.

  • On the dev environment php version is 5.6.27, production is 5.6.26.

  • OS versions are both CentOS release 6.8

  • php cURL info is as follows:



cURL support enabled
cURL Information 7.19.7
Age 3
Features
AsynchDNS No
CharConv No
Debug No
GSS-Negotiate Yes
IDN Yes
IPv6 Yes
krb4 No
Largefile Yes
libz Yes
NTLM Yes
SPNEGO No
SSL Yes
SSPI No
Protocols tftp, ftp, telnet, dict, ldap, ldaps, http, file, https, ftps, scp, sftp
Host x86_64-redhat-linux-gnu
SSL Version NSS/3.21 Basic ECC
ZLib Version 1.2.3
libSSH Version libssh2/1.4.2

Answer
Comments