cURL返回空字符串

时间:2015-09-24 03:44:18

标签: 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;
?>

1 个答案:

答案 0 :(得分:0)

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

$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