这是api:https://api.itemmaster.com/v2/api 但我不知道写下卷曲代码来获取UPC项目:00040000006039 我必须在哪里添加用户名和密码?
答案 0 :(得分:0)
CURL以及用户名和密码,并根据API文档设置为HTTP标头参数。
更改以下XXXX
的凭据$url ='https://api.itemmaster.com/v2/item/?upc=00040000006039'
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
curl_setopt($ch,CURLOPT_HTTPHEADER,array('username: XXXXX','password: XXXX'));
// OR
// curl_setopt($ch,CURLOPT_HTTPHEADER,array('username: XXXXX'));
// curl_setopt($ch,CURLOPT_HTTPHEADER,array('password: XXXX'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
答案 1 :(得分:0)
我建议使用Guzzle: http://guzzle.readthedocs.org/en/latest/
代码看起来像这样:
$client = new Client([
// Base URI is used with relative requests
'base_uri' => 'https://api.itemmaster.com/v2/',
// Set username and password in the header here
'headers' => ['username' => 'user_name', 'password' => 'thePassword']
]);
$response = $client->get('item',
[
'query' => ['idx' => '0','upc' => '00040000006039']
]);