gpt4 book ai didi

php - 无法在 php 内部使用 curl 调用 httprequest

转载 作者:太空宇宙 更新时间:2023-11-04 12:10:46 25 4
gpt4 key购买 nike

我设置了一个要玩的 symfony 项目,然后在 Controller 中创建了两条路线。在路由 1 中,使用 curl 调用路由 2。如下所示:

/**
* @Route("/", name="user_list")
*/
public function indexAction(Request $request)
{

$url = 'http://localhost:8000/test';
$headers = array( 'Authorization' => 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=', 'Content-Type' => 'application/json' );
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

$result = curl_exec($ch);
curl_close($ch);
var_dump( $result );

// replace this example code with whatever you need
return $this->render("user/user_list.html.twig");
}

/**
* @Route("/test", name="user_test")
*/
public function testAction(Request $request)
{
return new Response('abc', 200);
}

如果我单独运行路由器 2,它将返回 abc,但如果我运行路由器 1,它将一直等待连接。

我该如何解决?在 php 等、socket 中有什么要配置的吗...?有人可以帮帮我吗?谢谢

最佳答案

/**
* @Route("/", name="user_list")
*/
public function indexAction(Request $request)
{

$url = 'http://localhost:8000/test';
$headers = array( 'Authorization' => 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=', 'Content-Type' => 'application/json' );
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

$result = curl_exec($ch);
if($result === false) { // Get curl error with this
echo 'Curl error: ' . curl_error($ch);
}


curl_close($ch);
var_dump( $result );

// replace this example code with whatever you need
return $this->render("user/user_list.html.twig");
}

请引用here如上所述

关于php - 无法在 php 内部使用 curl 调用 httprequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49065719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com