PHP cURL发布请求不起作用 [英] PHP cURL Post request not working

查看:64
本文介绍了PHP cURL发布请求不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行正确的cURL请求时遇到问题.我打算对URL进行发布请求.

I having issues running the correct cURL request. I am meant to do a Post request to the URL.

该示例仅运行命令行cURL请求

The example only runs a command line cURL request

$ curl -i -X POST {URL}

该问题我正在运行以下代码,但收到"400错误的请求"

The issue I am running the following code and I am getting '400 Bad Request'

$ch = curl_init();
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
$output = curl_exec( $ch );
curl_close( $ch );

任何人都可以帮助正确发送请求.

Can anyone help with sending the request correctly.

推荐答案

您错过了要通过请求获得的 CURLOPT_POSTFIELDS .如此处所述,您将需要设置以下字段:

You're missing the CURLOPT_POSTFIELDS you wanna get by the request. As explained here you're gonna need to set those fields:

<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.example.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "postvar1=value1&postvar2=value2&postvar3=value3");

// in real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS, 
//          http_build_query(array('postvar1' => 'value1')));

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);

curl_close ($ch);

// further processing ....
if ($server_output == "OK") { ... } else { ... }

?>

这篇关于PHP cURL发布请求不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆