Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有标题和XML内容的PHP帖子_Php_Post_Youtube_Header_Gdata - Fatal编程技术网

带有标题和XML内容的PHP帖子

带有标题和XML内容的PHP帖子,php,post,youtube,header,gdata,Php,Post,Youtube,Header,Gdata,我想通过PHP通过GData API订阅YouTube频道 我如何用PHP写这篇文章 我试过这样做,但页面一直在加载: <?php session_start(); include 'gdata.php' // For $DEVKEY function post_xml($url, $xml) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER

我想通过PHP通过GData API订阅YouTube频道

我如何用PHP写这篇文章

我试过这样做,但页面一直在加载:

<?php

session_start();

include 'gdata.php' // For $DEVKEY

function post_xml($url, $xml) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, Array(
  "Content-Type: application/atom+xml",
  "Content-Length: 1024",
  "Authorization: Bearer $token",
  "GData-Version: 2",
  "X-GData-Key: key=$DEVKEY"));
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => $xml );
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

  $result = curl_exec($ch);
  $info = curl_getinfo($ch);
  curl_close($ch);
  return $result;
}    

$token = $_SESSION['token'];

$xml = '<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
  xmlns:yt="http://gdata.youtube.com/schemas/2007">
    <category scheme="http://gdata.youtube.com/schemas/2007/subscriptiontypes.cat"
      term="channel"/>
    <yt:username>GoogleDevelopers</yt:username>
</entry>';

$url = 'https://gdata.youtube.com/feeds/api/users/default/subscriptions';

echo post_xml($url, $xml);

?>

在HttpRequester中,我已经管理它执行了一个HTTP Post请求,它成功了。我认为问题在于请求的内容。如何通过PHP(cURL)正确地给出“Content”(查看屏幕截图)中的文本


谢谢:)

只需将
$xml
作为POSTFIELDS,它应该可以工作:

curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
并添加正确的内容长度:

"Content-Length: ".strlen($xml),

当你打算做更多的事情时,比如用数据放置请求和大量的REST请求,我建议你使用类似的包。Curl有它的陷阱…

您可以查看下面的代码,希望它能有所帮助

 $headers = array(
        "Content-type: text/xml",
        "Content-length: " . strlen($xml),
        "Connection: close",
    );

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $res = curl_exec($ch);
    curl_close($ch);

    return json_decode($res);

它一直在加载:/太糟糕了,我在使用相同的东西时遇到了麻烦(虽然API不同,但使用POST和XML数据),这为我解决了这个问题。API调用是否应该返回任何内容?因为你要求任何响应体(
CURLOPT_RETURNTRANSFER
),我可以想象只有HTTP 200响应。哦,我认为PHP仍然试图做所有的帖子,因为我的整个apache服务器都很慢。我重新启动了,它现在可以工作了。谢谢:)