1

I am trying to make a simple GET request to the yesmail api, i have the url which i can paste directly in the browser and it displays the correct information, and also if i add the following code as shown in the brief documentation in the Firefox RESTClient i get the correct response:

GET https://services.yesmail.com/enterprise/subscribers?email=karina@email.co.uk HTTP/1.1
Accept-Encoding: gzip,deflate
User-Agent: Jakarta Commons-HttpClient/3.1
Authorization: Basic xxxxxxxxxxxxx
Host: services.yesmail.com

However, when trying to connect using CURL, i am getting nothing, no HTTP response at all and just a blank page. I am not sure what i am doing wrong? This is what i have:

$url = "https://services.yesmail.com/enterprise/subscribers?email=karina@email.co.uk";  
$header[] = "Accept-Encoding: gzip, deflate";
$header[] = "User Agent: Jakarta Commons-HttpClient/3.1";
$header[] = "Authorization: Basic xxxxxxxxxxxxx";
$header[] = "Host: services.yesmail.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch, CURLOPT_USERPWD, 'xxxxx:xxxxxxxxxx');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
$response = (curl_exec($ch));
print_r($response);
curl_close($ch);

Am i wrong in thinking that the information i put in to the RESTClient goes into the headers in CURL? This is the first time i have been working with API's so any help appreciated.

update When using the function file_get_contents i get the correct response(which for the GET method is just a URL with my unique subscriber number):

 $context = stream_context_create(array(
'http' => array(
    'header'  => "Authorization: Basic " . base64_encode("*******:********")
)
));
$data = file_get_contents('https://services.yesmail.com/enterprise/subscribers?email=karina@email.co.uk', false, $context);

 echo $data;

However i really want to get the CURL method working as i will want to be able to add a subscriber.

Karina
  • 665
  • 6
  • 17
  • 35

2 Answers2

1

I actually work at Yesmail so I may be able to help you out here.

Give the following code a try (be sure to replace the your-auth-here text with your base64 encoded authentication info and to set the appropriate URL in $ym_api_url)

<?php

// Set the HTTP headers needed for this API call.
$http_headers = array(
    "Authorization: Basic your-auth-here",
    "Accept: application/json",
    "Connection: close",                    // Disable Keep-Alive
    "Expect:"                               // Disable "100 Continue" server response
);

// URL Endpoint of the API to be called.
$ym_api_url = 'https://services.yesmail.com/enterprise/subscribers?email=karina@email.co.uk';

$curl_hdl = curl_init();

$curl_options = array(
    CURLOPT_VERBOSE => 1,                 // Verbose mode for diagnostics
    CURLOPT_HEADER => TRUE,               // Include the header in the output.
    CURLOPT_HTTPHEADER => $http_headers,  // HTTP headers to set
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,   // Use basic authentication.
    CURLOPT_PROTOCOLS => CURLPROTO_HTTPS, // Bitmask of protocols libcurl may use in this transfer
    CURLOPT_RETURNTRANSFER => TRUE,       // Return result as return value of curl_exec()
    CURLOPT_URL => $ym_api_url,           // URL to POST data to
);

curl_setopt_array($curl_hdl, $curl_options);

// Make the API call
$response = curl_exec($curl_hdl);

// Get the http response code
$http_response_code = curl_getinfo($curl_hdl, CURLINFO_HTTP_CODE);

curl_close($curl_hdl);

echo PHP_EOL . "INFO: HTTP Response Code was: " . $http_response_code . PHP_EOL;

if ( $response === false )
{
    echo PHP_EOL . "ERROR: curl_exec() has failed." . PHP_EOL;
}
else
{
    echo PHP_EOL . "INFO: Response Follows..." . PHP_EOL;
    echo PHP_EOL . $response;
}

?>

The CURLOPT_VERBOSE => 1 option will output additional diagnostic info. Note any issues there to see if that points to a specific problem. Once you have the issue(s) worked out you can remove or disable those options.

Also, remember you can always contact your Yesmail Account Manager and request assistance.

Robert Groves
  • 7,574
  • 6
  • 38
  • 50
  • Great, thank you for your help, i used the code you gave and now i am getting something **INFO: HTTP Response Code was: 0 ERROR: curl_exec() has failed.** So HTTP Response 0, looked this up and found that this can sometimes happen when the call gets cancelled before getting a response? I tried using the function `file_get_contents` just to see if i could get any response from that and it works ok (i have updated my question with the code i used) – Karina Feb 05 '14 at 15:25
  • 1
    When executing this with the option CURLOPT_VERBOSE => 1 set you should have gotten some diagnostic messages (that are written to STDERR). Those messages (theoretically) should have given some indication as to what the issue is. Can you provide those diagnostic messages in an edit to your post? If executing this from a web page (and not the command line) you may have to go through some extra work to get the messages; see [this answer](http://stackoverflow.com/a/14436877) for info on how to do that. – Robert Groves Feb 06 '14 at 21:03
0

So you get the $response, but don't do anything with it.

There is no line in your code which is expected to display anything.

Update: I see you updated your question.

A very important rule: always check the return value of API calls.

curl_exec() signals an error by returning FALSE, if that's the case, check it with curl_error($ch).

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176