【问题标题】:How to write Curl api example into PHP curl如何将 Curl api 示例写入 PHP curl
【发布时间】:2016-07-17 00:14:20
【问题描述】:

我正在使用https://apptweak.io api, 他们在代码示例中没有提到 PHP 示例...

请告诉我,如何理解Curl API示例并将其写入PHP curl

这里是简单的“appteak.com” curl 示例

curl -G https://api.apptweak.com/ios/applications/284993459/informations.json \
-d country=us \
-d language=en \
-d device=iphone \
-H "X-Apptweak-Key: my_api_key"

这是另一个例子

$ curl -H "X-Apptweak-Key: my_api_key" \
https://api.apptweak.com/android/applications/com.facebook.katana.json?country=be&language=nl

如何在 PHP 中编写这些 curl?

我在 php 中做了这个,在 google 之后,但它不适合我。

// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://api.apptweak.com/android/applications/com.facebook.katana.json?country=be&language=nl',
   // CURLOPT_USERAGENT => 'Codular Sample cURL Request',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        'X-Apptweak-Key' => "my_api_key",
    )
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);


var_dump($resp ); //return false

【问题讨论】:

  • 你确定你传递了你的 API 密钥吗?
  • -H 用于标题。你应该使用CURLOPT_HTTPHEADER => array('X-Apptweak-Key','your key')
  • 另外你应该删除 CURLOPT_POST => 1 因为它是 GET 请求

标签: php api curl page-curl


【解决方案1】:

我找到正确答案

$token = "my_api_key";
$url = "http://URL.com";

$options = array('http' => array(
    'header' => array("X-Apptweak-Key: $token"),
));
$context = stream_context_create($options);

$result = file_get_contents($url, 0, $context);

【讨论】:

    猜你喜欢
    • 2017-12-03
    • 2016-09-27
    • 1970-01-01
    • 2019-09-01
    • 2011-01-09
    • 2015-10-21
    相关资源
    最近更新 更多