简体   繁体   English

如何使用php中的curl连接到API服务?

[英]How can I connect to a API service using curl in php?

I'm trying to connect to a API service using the following php: 我正在尝试使用以下php连接到API服务:

$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

However, when I run the following command from my command line in ubuntu: 但是,当我从ubuntu的命令行运行以下命令时:

jai@ubuntu:/opt/lampp$  curl -u api-key:my-api-key https://api.wlvpn.com/v2/customers

I get a response as expected 我得到预期的回应

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 谁能帮我我在这里缺少的东西,我想我缺少-u选项,但我不知道如何将其放在我的php代码中

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). 然后,您要告诉cURL连接到127.0.0.1上的代理(通常不存在)。 And the ssl certificate is self-signed, so you have to set CURLOPT_SSL_VERIFYHOST and CURLOPT_SSL_VERIFYPEER to 0 and false. 而且ssl证书是自签名的,因此您必须将CURLOPT_SSL_VERIFYHOST和CURLOPT_SSL_VERIFYPEER设置为0并设置为false。

This script works: 该脚本有效:

<?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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM