php - 我提出了一个 CURL 请求,但它不工作
问题描述
以下是我的 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 令牌。
解决方案
因为你$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
是一个array
因此,您可以通过其索引访问这些值。前任
$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'];
推荐阅读
- sql-server - 如何通过保护 Secret (Thycotic) Server 中的密钥来实现 SQL 中的列级加密?
- javascript - Elasticsearch 字符串匹配查询中的多个字段
- reactjs - 如何在 react native expo 中使用 styleSheet 应用样式
- java - java字符串replaceAll“到\\”
- python - keras中不同批量大小的训练模型
- c# - 缺少 HostingEnvironment.QueueBackgroundWorkItem 方法
- cypress - 仅为失败的测试用例录制视频
- javascript - 不确定为什么会发生“未捕获的 TypeError:无法读取属性 'setAttribute' of null”以及如何修复
- html - 如何停止悬停时的快速晃动
- .net-core - 基于资源的(命令式)授权不会进入处理程序