如何添加自定义标题到https请求curl PHP脚本? [英] How to add the custom headers to the https request curl PHP script?

查看:161
本文介绍了如何添加自定义标题到https请求curl PHP脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写以下代码

<?php
$ch=curl_init(); 
$h=array("Host:v.airtellive.com");
curl_setopt($ch,CURLOPT_URL,"https://facebook.com");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLINFO_HEADER_OUT,true);
curl_setopt($ch,CURLOPT_HEADER,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$h);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow header redirections
curl_setopt($ch, CURLOPT_MAXREDIRS, 4); // Limit redirections to 4
$p=curl_getinfo($ch);
$o=curl_exec($ch);
if($o==null)
var_dump (curl_error($ch)); 
$headers=curl_getinfo($ch,CURLINFO_HEADER_OUT);
print_r($headers);
echo "<xmp>";
echo $o;

curl_close($ch);
?>

但它给我一个错误:


无法解析主机

could not resolve the host

如果我使用此脚本与 search.yahoo .com 它工作正常。实际上,我想通过发送主机头创建realhost代理。

If I use this script with search.yahoo.com it works fine. Actually, I want to create the realhost proxy by sending the host headers.

推荐答案

CURL的标题设置如下



Headers for CURL mus be set like this

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_HTTPHEADER , array(
                "AUTH_USERNAME: {$username}",
                "AUTH_PASSWORD: {$password}",
            ));
$curlReturn = curl_exec($curl_handle);
    curl_close($curl_handle);

任何数量的自定义标题都可以作为数组发送。

Any number of custom headers can be sent as array in this.

这篇关于如何添加自定义标题到https请求curl PHP脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆