我有这个代码:
$httpParams = [
'textData' => $content,
'xmlFile' => new \CurlFile($params['file']->getPathName())
];
$curlHandle = curl_init('http://url.com');
curl_setopt($curlHandle, CURLOPT_HEADER, false);
curl_setopt($curlHandle, CURLOPT_POST, true);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $httpParams);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
$curlResponse = curl_exec($curlHandle);
curl_close($curlHandle);
dump($curlResponse);die();
我在
$curlResponse
中有回复,状态 = 200
但是当我尝试使用 Laravel 中的 Http 时:
$http = Http::asForm();
$httpParams = [
'textData' => $content,
'xmlFile' => new \CurlFile($params['file']->getPathName())
];
$response = $http->send('post', 'http://url.com', $httpParams)->body();
dump($response);
回复为空:
""
。状态为 200。为什么使用 Http Facade 时得到空响应?
您可以在单独的类中编写可重用的方法
public function apiCall($url, $method = "get",$data=[])
{
$htppCall = Http::withHeaders([
'Content-Type' => 'application/json',
])->{$method}($url,$data);
if ($htppCall->status() == 401) {
//error handling
}
return $htppCall->object();
}
然后这样打电话
$httpParams = [
'textData' => $content,
'xmlFile' => new \CurlFile($params['file']->getPathName())
];
$response=$this->apiCall($url,'post',$httpParams);
dd($response);
导入右立面
use Illuminate\Support\Facades\Http;
检查您获得的状态代码
$htppCall->status()
获取数据作为对象
$htppCall->object()
以数组形式获取数据
$htppCall->json()
检查客户端错误
$htppCall->clientError()
检查服务器错误
$htppCall->serverError()
获取响应正文。
$htppCall->body()
如果有任何问题请在评论中告诉我