php - 使用 PHP 和 Codeigniter 登录 API 失败
问题描述
我尝试使用此代码登录外部 API,但没有得到任何响应(什么都没有)。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://domain/rest/');
curl_setopt($ch, CURLOPT_POST, 1);// set post data to true
curl_setopt($ch, CURLOPT_POSTFIELDS,"username=MY_USERNAME&password=MY_PW");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close ($ch);
var_dump($json);
die("Stopp");
我做错了什么?还是有更好/更简单的登录方式?
同时更新我已将代码更改为此
curl_setopt($ch, CURLOPT_URL, 'https://shop.mydomain.com/rest/');
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"username=myapiuser&password=myapiuserpw");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
$headers = curl_getinfo($ch);
$errors = curl_error($ch);
curl_close ($ch);
echo"<hr>";
var_dump($json);
echo"<hr>";
var_dump($headers);
echo"<hr>";
var_dump($ch);
echo"<hr>";
var_dump($errors);
我在调用函数时收到这个
字符串(0)“”数组(30){[“url”]=>字符串(33)“ https://shop.mydomain.com/rest/" ["content_type"]=> 字符串(24) "text/html; charset=UTF-8" ["http_code"]=> int(200) ["header_size"]=> int(499) ["request_size"]=> int(170) ["filetime"]=> int(-1 ) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.275712) ["namelookup_time"]=> float(0.00649) ["connect_time"] => float(0.009913) ["pretransfer_time"]=> float(0.035844) ["size_upload"]=> float(37) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(134) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(37) ["starttransfer_time"]=> float(0.275399) ["
解决方案
尝试 :
$data = array('username' => 'MY_USERNAME', 'password' => 'MY_PW');
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
代替 :
curl_setopt($ch, CURLOPT_POSTFIELDS,"username=MY_USERNAME&password=MY_PW");
如果是客户端身份验证,请使用:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
我们需要更多信息来帮助您,什么是 API 之王,......如果它解决了您的问题,请不要忘记关闭帖子并接受答案。
推荐阅读
- javascript - javascript 函数运行时 HTML 页面不更新
- javascript - 使用 JavaScript 解析 html 标签
- html - CSS3 卡翻转故障
- android - 带页面指示器的无尽自动滚动视图寻呼机
- sql-server - SQL用值列表替换null
- c# - 拖动时将对象放在画布内
- javascript - 如何使用Javascript在单击事件的文本框中分别存储不同按钮的值?
- java - 投射到多边形 java Spring JPA
- docker - wso2 api manager Docker镜像需要付费订阅
- ios - 在 UITableview (Swift) 中将 Target 添加到 UITextField