0

因此,我尝试将以下表单中的值发布到 cURL url(脚本名称为 pagination.php),如下所示:

<form action="pagination.php" method="post">
<input style="width:50%" type="text" name="search_term">
<input type="submit" value="Submit">
</form>

$search_term = $_POST['search_term'];

echo $search_term;

$post_data = array('q' => $search_term);

$ch = curl_init();

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds
/api/videos?q='.urlencode($search_term).'&safeSearch=none&orderby=viewCount&
v=2&alt=json&start-index=1&max-results=50');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);

$data = json_decode($output,true);

$info = $data["feed"];
$video = $info["entry"];
$nVideo = count($video);



echo "<ul style='float:right'>";
foreach($video as $video) {
echo '<img src="'.$video['media$group']['media$thumbnail'][0]['url'].'"><br><br>';
$title = $video['title']['$t'];
$video_id = $video['media$group']['yt$videoid']['$t'];
echo '<a href="search_4.php?video_get_id='.$video_id.'">'.$title.'</a>';
echo '<br>'; 
}

当我像这样运行代码时,什么也没有发生(是的,我回显了 $search_term 以确保它不为空),但是如果我像这样手动为 $search_term 分配一个值

$search term = 'baseball';

一切正常。cURL 是否存在不允许发布数据的内容?谢谢!

4

2 回答 2

3

看起来你失踪了

curl_setopt($ch, CURLOPT_POST, 1 ); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 

编辑 现在回答未提出的问题“我如何使这项工作?”

就像@Kostanos 所说,看起来问题出在您的网址中的换行符。这有效:

<form action="pagination.php" method="post">
<input style="width:50%" type="text" name="search_term">
<input type="submit" value="Submit">
</form>
<?php
if($_POST)
  {
    $search_term = $_POST['search_term'];

    echo $search_term;

    $post_data = array('q' => $search_term);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos?q='.urlencode($search_term).'&safeSearch=none&orderby=viewCount&v=2&alt=json&start-index=1&max-results=50');
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $output = curl_exec($ch);
    curl_close($ch);

    $data = json_decode($output,true);

    $info = $data["feed"];
    $video = $info["entry"];
    $nVideo = count($video);



    echo "<ul style='float:right'>";
    foreach($video as $video)
    {
      echo '<img src="'.$video['media$group']['media$thumbnail'][0]['url'].'"><br><br>';
      $title = $video['title']['$t'];
      $video_id = $video['media$group']['yt$videoid']['$t'];
      echo '<a href="search_4.php?video_get_id='.$video_id.'">'.$title.'</a>';
      echo '<br>';
    }

  }
于 2013-05-28T01:53:11.687 回答
1

根据您的代码:

...
$search_term = $_POST['search_term'];
...
curl_setopt($ch, CURLOPT_POSTFIELDS, $search_term);
...

$search_form 不是正确的 POSTFIELDS 值,它应该是key=>value成对的数组或字符串,如下所示:&key=value&key1=value1..

尝试使用这个:

...
$search_term = $_POST['search_term'];
$post_data = array('q' => $search_term);
...
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
...

顺便说一句:对于 youtube API

您不需要 POST 请求,只需 GET 请求。从您的代码中删除这两行:

curl_setopt($ch, CURLOPT_POST, 1 ); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $search_term);

并替换这一行(确保它是一行,字符串中没有任何回车,看来您的编辑器在长字符串中添加了回车!):

curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos?q=' . urlencode($search_term) . '&safeSearch=none&orderby=viewCount&v=2&alt=json&start-index=1&max-results=50');

PS。通过将 urlencode 添加到 $search_term

于 2013-05-28T02:29:22.070 回答