php - cURL 返回空字符串

标签 php html json curl web

嘿,我是不是漏掉了 curl_opt 之类的东西?这段代码昨天还能用,现在不行了。它应该返回一个 json 字符串,但返回空字符串。

<?php
session_start();

$UCID=$_POST['UCID'];
$Pass=$_POST['Pass'];
$credentials=array("Username"=>$UCID, "Password"=>$Pass);
$post_field_string = http_build_query($credentials, '', '&');






$url1="https://web.njit.edu/~vm276/connect.php";
//$url1="https://www.njit.edu/cp/login.php";
$ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url1);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt ($ch, CURLOPT_REFERER, $url1);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_field_string);
    curl_setopt ($ch, CURLOPT_POST, true);
    $result = curl_exec($ch);
    curl_close($ch);
    $json=json_decode($result);
    echo $json->result;
?>

最佳答案

您可以通过添加到您的代码中来获取有关错误的信息:

$result= curl_exec($ch);
$error = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if(200 == $httpCode) {
  //...
}
else {
 echo "httpCode: ".$httpCode."\n";
 var_dump($error);
 exit();
}

http://php.net/manual/en/function.curl-error.php

http://php.net/manual/en/function.curl-getinfo.php

关于php - cURL 返回空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32752967/

相关文章:

php - 如何将父菜单和子菜单类设置为事件

php - ICU 编译器未找到或不适用于 PHP intl 扩展

php - jeditable、pdo 和 mysql

javascript - 表元素通过 javascript 连接过早关闭

javascript - 通过 Javascript MS Store UWP 应用程序中的代码截取屏幕截图

javascript - <canvas/> 并打印屏幕/屏幕截图

php - 表单定位 iframe 在 FireFox 中不工作

php - 使用 AJAX 将数据回显到下拉列表中

java - 如何使用 getJSONArray 方法访问 json 对象的嵌套元素

javascript - 如何维护 JSON 中的 <br/> 标签?