简体   繁体   English

使用curl不带命令行,但使用php作为post方法

[英]using curl without command line, but with php for a post method

I need to do a http post/curl query from php and store the result in a jsor variable. 我需要从php执行http post / curl查询,并将结果存储在jsor变量中。

In my documentation, it says there are two ways: 在我的文档中,它说有两种方法:

  • An HTTP POST request with Content-Type "multipart/form-data" where all parameters are in the post body and the track is in the "track" section of the post "files" HTTP POST请求,其内容类型为“ multipart / form-data”,其中所有参数都在帖子主体中,并且轨道在帖子“文件”的“轨道”部分中
  • An HTTP POST request with Content-Type "application/octet-stream", with the local file as the body of the request, and the parameters in the URL 内容类型为“ application / octet-stream”的HTTP POST请求,其中本地文件为请求的主体,URL中的参数

Example POSTs: 示例POST:

curl -X POST " http://developer.echonest.com/api/v4/track/upload " -d "api_key=xxxxxx&url= http://example.com/audio.mp3 " curl -X POST“ http://developer.echonest.com/api/v4/track/upload ” -d“ api_key = xxxxxx&url = http://example.com/audio.mp3

But how do I implement this in php? 但是如何在php中实现呢? I have no clue. 我没有任何线索。 From what I have been reading, I don;t think the curl_init method would work, like they describe on the php website, since it is a not a post method: 从我的阅读中,我不认为curl_init方法会像在php网站上描述的那样起作用,因为它不是post方法:

$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);

I would welcome any indication as how to proceed. 我欢迎任何指示如何进行。 Thanks. 谢谢。

How about this way? 这样呢

$post = array(
     "url"=>"path/to/file/example_homepage.txt",
     "api_key"=>"xxxxxx"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "http://developer.echonest.com/api/v4/track/upload");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
$response = curl_exec($ch);

There are other options, depending on the situation you might need them, SSL, Cookies, User Agents, etc... Here's a link to the php reference! 还有其他选项,具体取决于您可能需要的情况,SSL,Cookie,用户代理等。 这是php参考的链接!

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

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