【问题标题】:I've made a CURL request but its not working我提出了一个 CURL 请求,但它不工作
【发布时间】:2020-03-10 06:34:39
【问题描述】:

以下是我的 CURL 请求。我试图从名为 pipedrive 的 CRM 中提取数据。该 URL 正在正确提取数据,但由于某种原因它没有显示在我的网站上。

<?php
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.pipedrive.com/v1/persons/find?term=devonvermaak2%40gmail.com&start=0&search_by_email=1&api_token=e7f91c84dad486160a9744f4972d7f742de3d",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-length": "217",
    "content-type": "application/json",
    "x-ratelimit-limit": "20",
    "x-ratelimit-remaining": "19",
    "x-ratelimit-reset": "2"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);
//because of true, it's in an array
$response = json_decode($response, true); 
echo 'Name: '. $response['data']['name']. '<br />';
echo 'Email: '. $response['data']['email']. '<br />';
echo 'Phone: '. $response['data']['phone'];

它根本不显示数据。我的代码有问题吗?

注意:出于安全目的,我已经编辑了 api 令牌..

【问题讨论】:

  • API 中$response 的值是多少?
  • {"success":true,"data":[{"id":91235,"name":"Devon Vermaak","email":"devonvermaak2@gmail.com","phone ":"0555877857","org_id":null,"org_name":"","visible_to":"3"}],"additional_data":{"search_method":"search_by_email","pagination":{"start" :0,"limit":100,"more_items_in_collection":false}}}
  • data 元素是一个数组,所以需要$response['data'][0]['name']
  • 我已经添加了,但数据仍然没有显示
  • json_decode() 行之后通过print_r($response); 检查解码JSON。

标签: php api curl


【解决方案1】:

因为你的$response

{
  "success": true,
  "data": [
    {
      "id": 91235,
      "name": "Devon Vermaak",
      "email": "devonvermaak2@gmail.com",
      "phone": "0555877857",
      "org_id": null,
      "org_name": "",
      "visible_to": "3"
    }
  ],
  "additional_data": {
    "search_method": "search_by_email",
    "pagination": {
      "start": 0,
      "limit": 100,
      "more_items_in_collection": false
    }
  }
}

您可以看到dataarray

因此,您可以通过其索引访问这些值。例

$response['data'][0]['name'] 返回Devon Vermaak

echo 'Name: '. $response['data'][0]['name']. '<br />';
echo 'Email: '. $response['data'][0]['email']. '<br />';
echo 'Phone: '. $response['data'][0]['phone'];

【讨论】:

  • 是的,这正是我拥有它的方式,但它不显示数据。此外,当我 print_r $response 它不显示任何东西。
【解决方案2】:

错误的“CURLOPT_HTTPHEADER”数组值。希望对您有所帮助。

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.pipedrive.com/v1/persons/find?term=devonvermaak2%40gmail.com&start=0&search_by_email=1&api_token=e7f91c84dad486160a9744f4972d7f742de3d",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "cache-control"=> "no-cache",
    "content-length"=> "217",
    "content-type"=> "application/json",
    "x-ratelimit-limit"=> "20",
    "x-ratelimit-remaining"=> "19",
    "x-ratelimit-reset"=> "2"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);
//because of true, it's in an array
$response = json_decode($response, true); 
print_r($response);

【讨论】:

  • 如果您能解释一下错误的 'CURLOPT_HTTPHEADER' 数组值 是什么,将会很有帮助
  • 数组格式"cache-control":"no-cache" to "cache-control"=> "no-cache",
  • 引用手册,“CURLOPT_HTTPHEADER - 要设置的 HTTP 标头字段数组,格式为 array('Content-type: text/plain', 'Content-length: 100')
  • 所以不是'name' =&gt; 'value',而是'name: value'
猜你喜欢
  • 2015-03-08
  • 1970-01-01
  • 2018-06-04
  • 2019-08-13
  • 2018-03-18
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
相关资源
最近更新 更多