curl - 如何用php丟一個json到指定的http?
伊谢尔伦
伊谢尔伦 2017-04-11 09:48:06
0
3
298
$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;
}

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

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回覆(3)
黄舟
// 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);
PHPzhong

之前也遇到过这样的问题,共勉

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;
    }
阿神

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

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

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

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

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

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

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!