亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP 中的 HTTP POST

PHP 中的 HTTP POST

PHP
偶然的你 2022-06-11 18:46:34
我正在嘗試使用 PHP 發布 JSON 有效負載,但無法使其正常工作。我有以下適用于 Shell 的 CURL 命令以及以下也適用的 Python 代碼,但需要 PHP 實現相同的功能。殼牌上的卷曲:curl -H "Content-Type: application/json" -X POST -d '{"Content":" <CONTENT OF THE MESSAGE> "}' <URL of the node receiving POST data>Python:#!/usr/bin/python# -*- coding: utf-8 -*-import requestsheaders = {  'Content-type': 'application/json',}auth = '<URL>'line1 = '<CONTENT of message>'data = '{"Content":"%s"}' % (line1)response = requests.post(url = auth, headers = headers, data = data)到目前為止我在 PHP 中所擁有的(不工作):$data = array("Content" => "<CONTENT of the message>");                                                                    $data_string = json_encode($data);                                                                                   $ch = curl_init('<URL>');                                                                      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                              'Content-Type: application/json',                                                                                    'Content-Length: ' . strlen($data_string))                                                                       );                                                                                                                   $result = curl_exec($ch);提前非常感謝任何和所有幫助!
查看完整描述

2 回答

?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

試試這個代碼。


    $ch = curl_init( );

    $data = array("Content" => "<CONTENT of the message>");

    $headers = array(

    'Content-Type: application/json'

    );


    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

    curl_setopt ( $ch, CURLOPT_URL, $url );       

    curl_setopt ( $ch, CURLOPT_POST, 1 );

    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );

    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data);

    // Allow cUrl functions 20 seconds to execute

    curl_setopt ( $ch, CURLOPT_TIMEOUT, 20 );

    // Wait 10 seconds while trying to connect

    curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 );

    $output = array();

    $output['server_response'] = curl_exec( $ch );

    $curl_info = curl_getinfo( $ch );

    $output['http_status'] = $curl_info[ 'http_code' ];

    $output['error'] = curl_error($ch);

    curl_close( $ch );

    return $output;


查看完整回答
反對 回復 2022-06-11
?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

看起來你在那里做的一切都是正確的(除了第 10+11 行可怕的縮進,讓你看起來好像錯過了)實際上不是的),你只是缺少錯誤檢查代碼來調試它, 嘗試:


$stderrh=tmpfile();

curl_setopt_array($ch,[CURLOPT_VERBOSE=>1,CURLOPT_STDERR=>$stderrh]);

$result = curl_exec($ch);

rewind($stderrh); // https://bugs.php.net/bug.php?id=76268

var_dump(stream_get_contents($stderrh),$result);

詳細日志應該告訴你問題是什么,它說了什么?


(同樣你在<?php開始時錯過了,你可能想在最后添加。如果響應是可壓縮的(如 JSON 或 HTML 或文本),你還可以添加以使 curl 使用壓縮進行傳輸,var_dump($result);以加快速度,CURLOPT_ENCODING=>''這通常會加快速度)


查看完整回答
反對 回復 2022-06-11
  • 2 回答
  • 0 關注
  • 170 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號