为什么我的PHP CURL POST返回null结果

时间:2017-02-16 04:41:31

标签: php json curl postman

我在AWS的EC2实例中部署了一个Laravel v4 PHP应用程序。我的一个脚本使用CURL调用同一域中的POST路由,但没有得到任何结果。但是,如果我检查Postman中的POST路由,我会得到(JSON)结果

进行PHP CURL POST调用的代码如下:

$data_json = json_encode(array('company_id' => 100));
$url = 'http://somedomain.com/rest/getcompanyprofile';
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_json)));

$result = curl_exec($ch);
curl_close ($ch);
$obj = json_decode($result);

die(var_dump($obj));

$ result变量应该包含JSON返回值,而$ obj是$ result解码的字符串。 die语句显示NULL。

http://somedomain.com/rest/getcompanyprofile的路由调用以下代码:

public function fetchCompanyprofile(){
    $company_id = Input::get('company_id');

    $company_record = DB::table('company')->select('company_name', 'company_email', 'company_phone', 'company_CEO')->where('company_id', $company_id)->first();
    $company_name = $company_record->company_name;
    $company_email = $company_record->company_email;
    $company_CEO = $company_record->company_CEO;

    $response = array('company_name' => $company_name, 'company_email' => $company_email, 'company_CEO' => $company_CEO);

    return Response::json($response);
}

我正在使用Postman应用来测试http://somedomain.com/rest/getcompanyprofile。 Sofar,我得到了JSON结果。

我在 $ result = curl_exec($ ch);

行之后插入了以下代码
if(curl_error($ch)){
    die('CURL error: ' . curl_error($ch));
}

现在,我无法连接到http://somedomain.com:80;连接超时

我需要做些什么修复?

1 个答案:

答案 0 :(得分:0)

您是否创建了一个安全组,以便在实例之间允许TCP 80上的传入流量