简体   繁体   中英

Why does my PHP CURL POST return a null result

I have a Laravel v4 PHP app deployed in an EC2 instance in AWS. One my my script calls a POST route in the same domain using CURL but is not getting any result. But if I check on the POST route in Postman, I get the (JSON) result

The code that makes the PHP CURL POST call is below:

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

The $result variable should contain the JSON return value while $obj, the string decoded of $result. The die statement displays NULL.

The route for http://somedomain.com/rest/getcompanyprofile calls the code below:

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

I'm using Postman app to test http://somedomain.com/rest/getcompanyprofile . Sofar, Im getting the JSON result.

I've inserted the code below after the line $result = curl_exec($ch);

if(curl_error($ch)){
    die('CURL error: ' . curl_error($ch));
}

Now, I'm getting Failed to connect to http://somedomain.com:80 ; Connection time out

What fixes do I need to make?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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