0

PHP から http post/curl クエリを実行し、結果を jsor 変数に格納する必要があります。

私のドキュメントでは、次の 2 つの方法があると書かれています。

  • すべてのパラメーターが投稿本文にあり、トラックが投稿「files」の「track」セクションにある、Content-Type「multipart/form-data」の HTTP POST 要求
  • Content-Type が "application/octet-stream" で、ローカル ファイルが要求の本文であり、URL にパラメーターが含まれる HTTP POST 要求

投稿の例:

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

しかし、これをphpに実装するにはどうすればよいですか? 私は見当もつかない。私が読んだことから、ポストメソッドではないため、php Webサイトで説明されているように、curl_initメソッドが機能するとは思わない:

$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);

どのように進めるかについての指示を歓迎します。ありがとう。

4

1 に答える 1

4

この先どう?

$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);

必要な状況に応じて、SSL、Cookie、ユーザー エージェントなどの他のオプションがあります 。PHP リファレンスへのリンクは次のとおりです。

于 2013-06-19T16:32:40.290 に答える