2014-01-28 60 views
1

我想对yesmail api做一个简单的GET请求,我有我可以直接在浏览器中粘贴的url,它显示正确的信息,并且如果我将下面的代码添加为在Firefox的RESTClient实现了简短的文件中显示,我得到正确的响应:使用PHP进行GET请求

GET https://services.yesmail.com/enterprise/[email protected] HTTP/1.1 
Accept-Encoding: gzip,deflate 
User-Agent: Jakarta Commons-HttpClient/3.1 
Authorization: Basic xxxxxxxxxxxxx 
Host: services.yesmail.com 

然而,试图使用curl,我得到什么,根本没有HTTP响应,只是一个空白页相连的。我不确定我做错了什么?这是我有:

$url = "https://services.yesmail.com/enterprise/[email protected]"; 
$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); 

我错在想,我把到RESTClient实现的信息进入的卷曲头?这是我第一次使用API​​的,所以任何帮助表示赞赏。

更新 当使用功能file_get_contents我得到正确的响应(这对于GET方法仅仅是一个与我的唯一用户号码网址):

$context = stream_context_create(array(
'http' => array(
    'header' => "Authorization: Basic " . base64_encode("*******:********") 
) 
)); 
$data = file_get_contents('https://services.yesmail.com/enterprise/[email protected]', false, $context); 

echo $data; 

但是我真的想卷曲方法工作,因为我希望能够添加订阅者。

回答

1

我实际上在Yesmail工作,所以我可以在这里帮助你。

给下面的代码一试(一定要更换your-auth-here文字时,将base64编码认证信息,并设置相应的URL中$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/[email protected]'; 

$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; 
} 

?> 

CURLOPT_VERBOSE => 1选项将输出额外的诊断信息。注意那里的任何问题,看看是否指向特定的问题。解决问题后,您可以删除或禁用这些选项。

此外,请记住,您可以随时联系您的Yesmail客户经理并要求帮助。

+0

好,谢谢你的帮助,我用你给的代码,现在我得到了一些东西**信息:HTTP响应代码是:0错误:curl_exec()失败**所以HTTP响应0,看着这个和发现在获得响应之前呼叫被取消时,有时会发生这种情况?我尝试使用'file_get_contents'函数来查看是否可以得到任何响应,并且它工作正常(我用我使用的代码更新了我的问题) –

+1

当使用选项CURLOPT_VERBOSE => 1执行此操作时,您应该有得到了一些诊断消息(写入STDERR)。这些信息(理论上)应该给出一些关于问题是什么的指示。您可以在编辑中将这些诊断消息提供给您的文章吗?如果从网页(而不是命令行)执行此操作,则可能需要经过一些额外的工作才能获取消息;看到[这个答案](http://stackoverflow.com/a/14436877)了解如何做到这一点。 –

+0

我现在工作! :) 非常感谢你的帮助。 –

0

所以你得到$response,但不要做任何事情。

您的代码中没有预计显示任何内容的行。

更新:我看到你更新了你的问题。

一个非常重要的规则:总是检查API调用的返回值。

curl_exec()通过返回FALSE指示错误,如果是这种情况,请使用curl_error($ch)进行检查。

+0

好吧,我补充说,但我仍然只是返回一个空白页?没有错误,没有。 –

+0

你在浪费我们的时间。请提供重现问题的代码。 –