是否可以将数据发布到卷曲网址?

时间:2013-05-28 01:33:15

标签: php json curl

所以我试图将以下表单中的值发布到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有什么不允许发布的数据吗?谢谢!

2 个答案:

答案 0 :(得分: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>';
    }

  }

答案 1 :(得分: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);
...

BTW:适用于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