如何在php中使用api.ssactivewear.com获取产品数据?

时间:2016-12-27 04:39:45

标签: php php-curl

通过使用api.ssactivewear.com,示例中没有php代码选项(https://api.ssactivewear.com/V2/Help_Examples.aspx)。任何帮助或支持都可以解决我的问题。我在php中完成了下面提到的代码来获取xml中的数据:

ini_set("allow_url_fopen", 1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.ssactivewear.com/v2/products/B00760003?username=xxxxx&api_key=xxxxxxxxxxxxxxxxxx');
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);

我得到以下结果: - string(68)

"{ "message": "Authorization has been denied for this request." }"

2 个答案:

答案 0 :(得分:1)

得到了解决方案,它对我有用:

$username = "xxxxx";
$password = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
$remote_url = 'https://api.ssactivewear.com/v2/categories/';

// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header' => "Authorization: Basic " . base64_encode("$username:$password")            
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above

$json = file_get_contents($remote_url, false, $context);

$json=str_replace('},
]',"}
]",$json);

$data = json_decode($json, true);

var_dump($data);

答案 1 :(得分:0)

我必须找到解决方案。 以下所有代码都适用于我。

$apiPath =  "https://api.ssactivewear.com/v2/products";
$username = 'XXXXXXXX';
$password = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXx';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$apiPath);
$headers = array(
    'Content-Type:text/html'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);