curl - 如何用php丟一個json到指定的http?

 三喜金融 发布于 2022-11-15 06:07
$url = 'http://xxx.xxx.xxx.xxx:x/xxx';
$ch = curl_init($url);
$payload = array(
  'message' => 'notification with php curl'
);
$jsonDataEncoded = json_encode($payload);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($ch);
curl_close($ch);

我用了這個方式想用php 丟 json 到我指定的url...
只是怎麼試都無法運行
就會loading很久,到最後就是空白頁(而server端什麼事也沒發生)

// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://testcURL.com',
    CURLOPT_USERAGENT => 'Codular Sample cURL Request',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        item1 => 'value',
        item2 => 'value2'
    )
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
$r = new HttpRequest("http://xxx:xxx/", HttpRequest::METH_POST);
$r->setOptions(array("message" => "123"));
try {
    echo $r->send()->getBody();
} catch (HttpException $ex) {
    echo $ex;
}

試了很多種都還是失敗.....

3 个回答
  • 代码都没错的话,题主可以检查几个地方:

    1. 请求的参数有没有问题,缺了什么或者少了什么,数据的格式等

    2. 请求的url那里有没有加权限验证

    3. 请求的服务器是否正常使用

    4. 再一个就是检查一下curl服务有没有开启(如果有开启的话请忽视这条)

    如果可以的话,题主可以贴一下服务端代码吗?

    2022-11-15 08:42 回答
  • 之前也遇到过这样的问题,共勉

    class RestClient
    {
    
        public function get($url, $parameters = array(), $headers = array())
        {
            return $this->execute($url, 'GET', $parameters, $headers);
        }
    
        public function post($url, $parameters = array(), $headers = array())
        {
            return $this->execute($url, 'POST', $parameters, $headers);
        }
    
        public function put($url, $parameters = array(), $headers = array())
        {
            return $this->execute($url, 'PUT', $parameters, $headers);
        }
    
        public function delete($url, $parameters = array(), $headers = array())
        {
            return $this->execute($url, 'DELETE', $parameters, $headers);
        }
    
        public function execute($url, $method = 'GET', $parameters = array(), $headers = array())
        {
    
            $ch = curl_init();
            $data_json = json_encode($parameters);
            curl_setopt($ch, CURLOPT_URL, $url);
            $upper_method = strtoupper($method);
            if ($upper_method == 'POST') {
                curl_setopt($ch, CURLOPT_POST, true);
            } else if ($upper_method != 'GET') {
                curl_setopt($ch, CURLOPT_HTTPGET, true);
            } else if ($upper_method != 'PUT') {
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
            } else if ($upper_method != 'DELETE') {
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
            }
            if ($headers === NULL) {
                curl_setopt($ch,
                    CURLOPT_HTTPHEADER,
                    array('Content-Type: application/json',
                        'Content-Length: ' . strlen($data_json)));
            } else {
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            }
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
            $response = curl_exec($ch);
            curl_close($ch);
            return $response;
        }
    2022-11-15 08:42 回答
  • // Get cURL resource
    $curl = curl_init();
    // Set some options - we are passing in a useragent too here
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => 'http://testcURL.com',
        CURLOPT_USERAGENT => 'Codular Sample cURL Request',
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => array(
            item1 => 'value',
            item2 => 'value2'
        )
    ));
    // Send the request & save response to $resp
    $resp = curl_exec($curl);
    // Close request to clear up some resources
    curl_close($curl);
    2022-11-15 08:42 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有