Php Laravel中的卷曲请求
我正在努力在拉雷维尔提出这个卷发要求Php Laravel中的卷曲请求,php,laravel,curl,guzzle,Php,Laravel,Curl,Guzzle,我正在努力在拉雷维尔提出这个卷发要求 curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X GET http://my.domain.com/test.php 我一直在尝试: $endpoint = "http://my.domain.com/test.php"; $client = new \GuzzleHttp\Client(); $response = $client
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X GET http://my.domain.com/test.php
我一直在尝试:
$endpoint = "http://my.domain.com/test.php";
$client = new \GuzzleHttp\Client();
$response = $client->post($endpoint, [
GuzzleHttp\RequestOptions::JSON => ['key1' => $id, 'key2' => 'Test'],
]);
$statusCode = $response->getStatusCode();
但是我得到了一个错误,Class'App\Http\Controllers\GuzzleHttp\RequestOptions'未找到
有什么建议吗
编辑
我需要从API中获取
$response
中的响应,然后将其存储在DB中。。。我该怎么做/ 尝试一下Guzzle的查询选项:
$endpoint = "http://my.domain.com/test.php";
$client = new \GuzzleHttp\Client();
$id = 5;
$value = "ABC";
$response = $client->request('GET', $endpoint, ['query' => [
'key1' => $id,
'key2' => $value,
]]);
// url will be: http://my.domain.com/test.php?key1=5&key2=ABC;
$statusCode = $response->getStatusCode();
$content = $response->getBody();
// or when your server returns json
// $content = json_decode($response->getBody(), true);
我使用此选项使用guzzle构建get请求。结合json_decode($json_value,true),您可以将json转换为php数组。如果您在使用HTTP时遇到问题,您仍然可以在php中使用本机cURL: 本机Php方式
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "SOME_URL_HERE".$method_request);
// SSL important
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch);
curl_close($ch);
$this - > response['response'] = json_decode($output);
有时,这种解决方案比使用Laravel框架中附带的库更好、更简单。但仍然是您的选择,因为您负责项目的开发。将此作为参考。我已经用这段代码成功地发出了curl-GET请求
public function sendSms($mobile)
{
$message ='Your message';
$url = 'www.your-domain.com/api.php?to='.$mobile.'&text='.$message;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec ($ch);
$err = curl_error($ch); //if you need
curl_close ($ch);
return $response;
}
使用Laravel,如果您正在使用WP,并且您感到有冒险精神,并且不想使用guzzle或Laravel curl包,那么您可以在routes文件中编写类似的内容
Route::get('/curl',function() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.net/wp-login.php');
// save cookies to 'public/cookie.txt' you can change this later.
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, ['log'=>'<name>','pwd'=>'<pass>']);
curl_exec($ch);
// supply cookie with request
curl_setopt($ch, CURLOPT_COOKIE, 'cookie.txt');
// the url you would like to visit
curl_setopt($ch, CURLOPT_URL, 'https://example.net/profile/');
$content = curl_exec($ch);
curl_close($ch);
// webpage will be displayed in your browser
return;
});
Route::get('/curl',function(){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,'https://example.net/wp-login.php');
//将cookie保存到“public/cookie.txt”,您可以稍后更改。
curl_setopt($ch,CURLOPT_COOKIEJAR,'cookie.txt');
curl_setopt($ch,CURLOPT_POSTFIELDS,['log'=>'','pwd'=>'');
curl_exec($ch);
//向cookie提供请求
curl_setopt($ch,CURLOPT_COOKIE,'COOKIE.txt');
//您要访问的url
curl_setopt($ch,CURLOPT_URL,'https://example.net/profile/');
$content=curl\u exec($ch);
卷曲关闭($ch);
//网页将显示在您的浏览器中
返回;
});
您忘记在名称空间之前添加\了
你应该写:
$response = $client->post($endpoint, [
\GuzzleHttp\RequestOptions::JSON => ['key1' => $id, 'key2' => 'Test'],
]);
而不是:
$response = $client->post($endpoint, [
GuzzleHttp\RequestOptions::JSON => ['key1' => $id, 'key2' => 'Test'],
]);
在一个地方使用\GUZLEHTTP,在另一个地方使用GuzzHttp(无反斜杠)。也许这是一个问题啊,这是。。。但现在我意识到我是在大口大口地发出
POST
请求。我需要获取
并将这些值存储在DB中。也许你应该看看数据库。此选项转换给定值以获取参数(基于键值)。请尝试$response=$response->getBody()当服务器返回json时,code>或json\u解码($response->getBody(),true)
。