简体   繁体   English

在PHP中翻译Ruby API调用

[英]Translating Ruby API call in PHP

I am trying to access an API, but the instructions I have been given are written for ruby, which I am not familiar with. 我正在尝试访问API,但是给出的说明是针对红宝石编写的,我并不熟悉。 I am coding in PHP and was after some assistance in translating the instructions: 我使用PHP进行编码,并在翻译说明后得到了一些帮助:

GET /webservice/parties HTTP/1.1
Host: http://subdomain.domain.com
Authorization: Basic YXBpOmZhYTFmYmMwN2Q1MTczNTI3NGFj    

Additional instructions: username being sent should be “api” 附加说明:发送的用户名应为“ api”

Apologies if the question is rudimentary, unclear or already answered somewhere else. 如果问题是基本问题,不清楚的问题或在其他地方已经回答的问题,我们深表歉意。 And thanks in advance. 并预先感谢。

Try something like: 尝试类似:

$username = 'api';
$password = 'YXBpOmZhYTFmYmMwN2Q1MTczNTI3NGFj';

$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://subdomain.domain.com/webservice/parties',
    CURLOPT_USERAGENT => 'Stack Overflow Example Agent',
    CURLOPT_USERPWD => $username . ":" . $password
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

You might also want to check How do I make a request using HTTP basic authentication with PHP curl? 您可能还需要检查如何使用带有PHP curl的HTTP基本身份验证进行请求?

I personally prefer to use the Guzzle Client Library which might look like: 我个人更喜欢使用Guzzle客户端库 ,该可能类似于:

use GuzzleHttp\Client;

$username = 'api';
$password = 'YXBpOmZhYTFmYmMwN2Q1MTczNTI3NGFj';

$client = new Client([
    'base_url' => ['http://subdomain.domain.com/webservice/parties'],
    'defaults' => [
        'auth'    => [$username, $password]
    ]
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM