使用google url shortner api的php curl帮助
我正在尝试使用GGOLEAPI缩短url。这是我的php代码。当我加载时,它会给出一个空白页面使用google url shortner api的php curl帮助,php,json,curl,Php,Json,Curl,我正在尝试使用GGOLEAPI缩短url。这是我的php代码。当我加载时,它会给出一个空白页面 <?php define('GOOGLE_API_KEY', 'GoogleApiKey'); define('GOOGLE_ENDPOINT', 'https://www.googleapis.com/urlshortener/v1'); function shortenUrl($longUrl) { // initialize the cUR
<?php
define('GOOGLE_API_KEY', 'GoogleApiKey');
define('GOOGLE_ENDPOINT', 'https://www.googleapis.com/urlshortener/v1');
function shortenUrl($longUrl)
{
// initialize the cURL connection
$ch = curl_init(
sprintf('%s/url?key=%s', GOOGLE_ENDPOINT, GOOGLE_API_KEY)
);
// tell cURL to return the data rather than outputting it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// create the data to be encoded into JSON
$requestData = array(
'longUrl' => $longUrl
);
// change the request type to POST
curl_setopt($ch, CURLOPT_POST, true);
// set the form content type for JSON data
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
// set the post body to encoded JSON data
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));
// perform the request
$result = curl_exec($ch);
curl_close($ch);
// decode and return the JSON response
return json_decode($result, true);
}
if (isset($_POST['url'])) {
$url = $_POST['url'];
$response = shortenUrl('$url');
echo sprintf(
$response['longUrl'],
$response['id']
);
}
?>
我的html文件:
<html>
<head>
<title>A BASIC HTML FORM</title>
</head>
<body>
<FORM NAME ="form1" METHOD =" " ACTION = "shortner.php">
<INPUT TYPE = "TEXT" VALUE ="Enter a url to shorten" name="url">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Shorten">
</FORM>
</body>
</html
一个基本的HTML表单
您需要在代码中设置CURLOPT_RETURNTRANSFER选项
function shortenUrl($longUrl)
{
// initialize the cURL connection
$ch = curl_init(
sprintf('%s/url?key=%s', GOOGLE_ENDPOINT, GOOGLE_API_KEY)
);
// tell cURL to return the data rather than outputting it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// create the data to be encoded into JSON
$requestData = array(
'longUrl' => $longUrl
);
// change the request type to POST
curl_setopt($ch, CURLOPT_POST, true);
// set the form content type for JSON data
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
// set the post body to encoded JSON data
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// perform the request
$result = curl_exec($ch);
curl_close($ch);
// decode and return the JSON response
return json_decode($result, true);
}
if (isset($_POST['url'])) {
$url = $_POST['url'];
$response = shortenUrl('$url');
echo sprintf(
$response['longUrl'],
$response['id']
);
}
您需要在代码中设置CURLOPT_RETURNTRANSFER选项
function shortenUrl($longUrl)
{
// initialize the cURL connection
$ch = curl_init(
sprintf('%s/url?key=%s', GOOGLE_ENDPOINT, GOOGLE_API_KEY)
);
// tell cURL to return the data rather than outputting it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// create the data to be encoded into JSON
$requestData = array(
'longUrl' => $longUrl
);
// change the request type to POST
curl_setopt($ch, CURLOPT_POST, true);
// set the form content type for JSON data
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
// set the post body to encoded JSON data
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// perform the request
$result = curl_exec($ch);
curl_close($ch);
// decode and return the JSON response
return json_decode($result, true);
}
if (isset($_POST['url'])) {
$url = $_POST['url'];
$response = shortenUrl('$url');
echo sprintf(
$response['longUrl'],
$response['id']
);
}
curl\u exec()
如果请求不正确,则返回布尔值false。你不是在测试它,而是假设它起作用。将代码更改为:
$result = curl_exec($ch);
if ($result === FALSE) {
die("Curl error: " . curl_error($ch);
}
此外,您还需要指定CURLOPT_RETURNTRANSFER——默认情况下,curl会将接收到的任何内容写入PHP输出。设置此选项后,它会将传输返回到$result变量,而不是将其写出。curl\u exec()
如果请求不正确,则返回布尔值false。你不是在测试它,而是假设它起作用。将代码更改为:
$result = curl_exec($ch);
if ($result === FALSE) {
die("Curl error: " . curl_error($ch);
}
此外,您还需要指定CURLOPT_RETURNTRANSFER——默认情况下,curl会将接收到的任何内容写入PHP输出。设置此选项后,它会将传输返回到$result变量,而不是将其写出。我认为它来自html。您没有放置表单methode,因此它通过get发送数据
只有当你有帖子的时候,你才会展示一些东西
尝试以method=“post”的形式执行
编辑
Bobby主要的问题是在这个代码中你没有一个问题,但是有几个问题。
首先,如果你不这样做
<FORM NAME="form1" METHOD="POST" ACTION="shortner.php">
Sprintf需要采用字符串格式:
echo sprintf("%s %s" // for example
$response['longUrl'],
$response['id']
);
但是你知道你可以直接做吗
echo $response['longUrl'] . ' ' . $response['id'];
您可以将字符串直接连接到。在php中,我认为它来自您的html。您没有放置表单methode,因此它通过get发送数据
只有当你有帖子的时候,你才会展示一些东西
尝试以method=“post”的形式执行
编辑
Bobby主要的问题是在这个代码中你没有一个问题,但是有几个问题。
首先,如果你不这样做
<FORM NAME="form1" METHOD="POST" ACTION="shortner.php">
Sprintf需要采用字符串格式:
echo sprintf("%s %s" // for example
$response['longUrl'],
$response['id']
);
但是你知道你可以直接做吗
echo $response['longUrl'] . ' ' . $response['id'];
您可以将字符串直接连接到。在php中,我想我已经找到了解决您问题的方法。由于您要连接到使用SSL的URL,因此需要为CURL的代码添加一些额外的参数。请尝试以下操作:
<?php
define('GOOGLE_API_KEY', 'GoogleApiKey');
define('GOOGLE_ENDPOINT', 'https://www.googleapis.com/urlshortener/v1');
function shortenUrl($longUrl)
{
// initialize the cURL connection
$ch = curl_init(
sprintf('%s/url?key=%s', GOOGLE_ENDPOINT, GOOGLE_API_KEY)
);
// tell cURL to return the data rather than outputting it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// create the data to be encoded into JSON
$requestData = array(
'longUrl' => $longUrl
);
// change the request type to POST
curl_setopt($ch, CURLOPT_POST, true);
// set the form content type for JSON data
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
// set the post body to encoded JSON data
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));
// extra parameters for working with SSL URL's; eypeon (stackoverflow)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// perform the request
$result = curl_exec($ch);
curl_close($ch);
// decode and return the JSON response
return json_decode($result, true);
}
if (isset($_POST['url'])) {
$url = $_POST['url'];
$response = shortenUrl('$url');
echo sprintf(
$response['longUrl'],
$response['id']
);
}
?>
我想我已经找到了解决你问题的办法。由于您要连接到使用SSL的URL,因此需要为CURL的代码添加一些额外的参数。请尝试以下操作:
<?php
define('GOOGLE_API_KEY', 'GoogleApiKey');
define('GOOGLE_ENDPOINT', 'https://www.googleapis.com/urlshortener/v1');
function shortenUrl($longUrl)
{
// initialize the cURL connection
$ch = curl_init(
sprintf('%s/url?key=%s', GOOGLE_ENDPOINT, GOOGLE_API_KEY)
);
// tell cURL to return the data rather than outputting it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// create the data to be encoded into JSON
$requestData = array(
'longUrl' => $longUrl
);
// change the request type to POST
curl_setopt($ch, CURLOPT_POST, true);
// set the form content type for JSON data
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
// set the post body to encoded JSON data
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));
// extra parameters for working with SSL URL's; eypeon (stackoverflow)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// perform the request
$result = curl_exec($ch);
curl_close($ch);
// decode and return the JSON response
return json_decode($result, true);
}
if (isset($_POST['url'])) {
$url = $_POST['url'];
$response = shortenUrl('$url');
echo sprintf(
$response['longUrl'],
$response['id']
);
}
?>
shortenUrl(“$url”)代码>需要使用双引号(或者根本没有)。请再次阅读我的答案,我现在只需编辑它。我想我解决了你所有的问题。我真的可以测试,因为我没有任何api密钥。但是它似乎起作用了。谢谢simon,我得到了它。hhey我得到了输出,但是当我从html文件中输入url时,它会去获取缩短的url。我如何捕获缩短的url并将其显示在html页面中。而不是像php OUPUTH那样,我如何将其输出到html文件中,在该文件中输入内容并在其下方显示缩短的urlshortenUrl(“$url”);
需要使用双引号(或根本不使用)。再次阅读我的答案,我现在就编辑它。我想我解决了你所有的问题。我真的可以测试,因为我没有任何api密钥。但它似乎可以工作。谢谢simon,我得到了它。hhey我得到了输出,但当我从html文件中输入url时,它会去获取缩短的url。我如何捕获缩短的url并将其显示在html页面中。而不是php OUPUTH我如何将其输出到html文件中,在该文件中输入并在itOP的表单提交下面显示缩短的url与问题无关-在他的表单处理程序上,他通过POST将请求提交给google的缩短者。当然,如果(isset($_POST['url']){$response=shortenUrl('$url');},他会这样做,因此,当您单击表单按钮时,您永远不会调用shortnUrl()函数。你可以做任何你想做的事,如果代码从来没有被调用…顺便说一句,鲍比,当东西不是那样工作的时候,你应该到处调试,看看你的代码执行的确切部分。OP的表单提交与问题无关-在他的表单处理程序上,他通过POST anywa将请求提交给谷歌的shorterner当然,当他这样做的时候,如果(isset($_POST['url']){$response=shortenUrl('$url');},那么当你点击表单按钮时,你永远不会调用shortnUrl()你可以做任何你想做的事,如果代码从未被调用…顺便说一句,bobby,当东西不是那样工作时,你应该到处调试,看看你的代码到底执行了哪一部分。json\u decode()
如果无法解码json数据,也会返回NULL,因此您可能还需要检查这种情况。google响应中可能有垃圾,您必须过滤掉,或者google返回某种纯文本(非json)响应。在尝试解码之前,可能要将$result
记录在某个地方。json\u decode()
如果无法解码json数据,也会返回NULL,因此您可能还需要检查这种情况。google响应中可能有垃圾,您必须过滤掉,或者google返回某种纯文本(非json)响应。在尝试解码之前,可能要将$result
记录在某个地方。