如何在PHP中使用curl连接到API服务?
我尝试使用以下php连接到API服务:
I'm trying to connect to a API service using the following php:
$url = 'https://api.wlvpn.com/v2/customers&api-key=my-api-key'
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, "http://127.0.0.1/");
$output = curl_exec($ch);
$curl_error = curl_error($ch);
curl_close($ch);
print_r($output);
print_r($curl_error);
当我运行它时,我得到以下错误:
when I run it I get the following error:
couldn't connect to host
当我从ubuntu的命令行运行以下命令:
However, when I run the following command from my command line in ubuntu:
jai@ubuntu:/opt/lampp$ curl -u api-key:my-api-key https://api.wlvpn.com/v2/customers
我得到了预期的响应
任何人都可以帮助我什么我在这里缺失
我认为我缺少-u选项,但我不知道如何把它放在我的php代码
Can anyone help me what I am missing here I think I am missing -u option but I dont have any idea how to put it on my php code
这是你预期的答案。网址不正确,因为您使用&代替 ?。然后你告诉cURL连接到一个代理在127.0.0.1(通常没有)。而且ssl证书是自签名的,因此您必须将CURLOPT_SSL_VERIFYHOST和CURLOPT_SSL_VERIFYPEER设置为0和false。
Here is your expected answer. The url isn't correct, because you're using & instead of ?. And then you're telling cURL to connect to a proxy on 127.0.0.1 (there is none, usually). And the ssl certificate is self-signed, so you have to set CURLOPT_SSL_VERIFYHOST and CURLOPT_SSL_VERIFYPEER to 0 and false.
此脚本适用于:
<?php
$url = 'https://api.wlvpn.com/v2/customers?api-key=my-api-key';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
$curl_error = curl_error($ch);
curl_close($ch);
print_r($output);
print_r($curl_error);
?>