简体   繁体   English

PHP如何使用CURL

[英]PHP how to use CURL

I need to do this command 我需要执行此命令

root@debian:~# curl -X PUT -d '{ "date": "2.5.", "order": 2, "prize": 45 }' '[URL]'

in PHP (or Python). 在PHP(或Python)中。 But I have no idea how to do it. 但是我不知道该怎么做。 I tried this: 我尝试了这个:

$data = '{ "date": "2.5.", "order": 2, "prize": 45 }';
$data = json_encode($data);
echo $data;
$ch = curl_init([URL]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
curl_close($ch);
echo $response;

But this returns: 但这返回:

{ "error" : "Error: No data supplied." }

Any idea how to reproduce it in PHP/Python? 知道如何在PHP / Python中重现它吗?

$url = "[URL]";

$data = array(
"date" => "2.5.",
"order" => "2",
"prize" => 45
);

$json_data = json_encode($data);

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$content = curl_exec($ch);

curl_close($ch);

subprocess.call or subprocess.run will do what you need for this, although the output will get dumped to stdout, so you'll also need to redirect it if you want to manipulate the returned data. 尽管输出将转储到stdout,subprocess.call或subprocess.run会完成您需要的操作,因此,如果您要操作返回的数据,还需要重定向它。 However, you could also use requests , like some other commenters have suggested. 但是,您也可以使用request ,就像其他评论者建议的那样。

https://docs.python.org/3.5/library/subprocess.html https://docs.python.org/3.5/library/subprocess.html

import subprocess
import tempfile

with tempfile.TemporaryFile() as tmp:
    subprocess.call(["curl", "-X", "PUT", "-d", '{ "date": "2.5.", "order": 2, "prize": 45 }', '[URL]'], stdout=tmp)
    tmp.seek(0)
    data = tmp.read()

Accoridng to the PHP docs, 根据PHP文档,

http://php.net/manual/en/function.http-build-query.php http://php.net/manual/zh/function.http-build-query.php

http_build_query accepts an array or object as the paramater. http_build_query接受数组或对象作为参数。 Also, json_encode returns a string: 另外, json_encode返回一个字符串:

http://php.net/manual/en/function.json-encode.php http://php.net/manual/zh/function.json-encode.php

So, you need to change the way you are using them. 因此,您需要更改使用它们的方式。

Further, using http_build_query might be preferable since it url-encodes the given params: 此外,最好使用http_build_query因为它会对给定的参数进行url编码:

When POSTing an associative array with cURL, do I need to urlencode the contents first? 使用cURL发布关联数组时,是否需要先对内容进行urlencode?

So you need to pass an array to the http_build_query function to make it work: 因此,您需要将数组传递给http_build_query函数以使其起作用:

Example (from above link): 示例(从上面的链接):

  $curl_parameters = array(
    'param1' => $param1,
    'param2' => $param2
  );

  $curl_options = array(
    CURLOPT_URL => "http://localhost/service",
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => http_build_query( $curl_parameters ),
    CURLOPT_HTTP_VERSION => 1.0,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => false
  );

  $curl = curl_init();
  curl_setopt_array( $curl, $curl_options );
  $result = curl_exec( $curl );

  curl_close( $curl );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM