实际上我想读完搜索查询后的内容.问题是URL只接受POST方法,并且不对GET方法采取任何操作......
我必须在domdocument或的帮助下阅读所有内容file_get_contents().是否有任何方法可以让我用POST方法发送参数然后通过读取内容PHP?
PHP中这两个函数调用有什么区别?
init_get($somevariable);
@init_get($somevariable);
Run Code Online (Sandbox Code Playgroud) 我正在尝试file_get_contents与stream_context_createPOST 一起使用.我的代码到目前为止:
$options = array('http' => array(
'method' => 'POST',
'content' => $data,
'header' =>
"Content-Type: text/plain\r\n" .
"Content-Length: " . strlen($data) . "\r\n"
));
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
Run Code Online (Sandbox Code Playgroud)
它工作正常,但是,当发生HTTP错误时,它会发出警告:
file_get_contents(...): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
Run Code Online (Sandbox Code Playgroud)
并返回false.有办法:
我想使用PHP下载URL的内容,即使HTTP响应代码是404. file_get_contents将出错,我无法使用Google找到答案.我怎样才能做到这一点?
我成功地获取网站
file_get_contents("http://www.site.com");
Run Code Online (Sandbox Code Playgroud)
但是,如果网址不存在或无法访问,我会收到
Warning: file_get_contents(http://www.site.com) [function.file-get-contents]:
failed to open stream: operation failed in /home/track/public_html/site.php
on line 773
Run Code Online (Sandbox Code Playgroud)
是否可以echo "Site not reachable";代替错误?
我正在尝试使用file_get_contents函数获取图像,但它给出了错误。为了处理错误,我使用了try catch块,但它没有捕获错误并失败。
我的代码:
try {
$url = 'http://wxdex.ocm/pdd.jpg'; //dummy url
$file_content = file_get_contents($url);
}
catch(Exception $e) {
echo 'Error Caught';
}
Run Code Online (Sandbox Code Playgroud)
错误:
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: No such host is known
Warning: file_get_contents(http://wxdex.ocm/pdd.jpg): failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known.
Run Code Online (Sandbox Code Playgroud)
注意::我能够在遥控器上获取任何其他有效的图像URL。
当我使用如下所示的有效URL时,file_get_contents将返回html代码
$html = file_get_contents("http://www.google.com/");
Run Code Online (Sandbox Code Playgroud)
但如果网址无效:
$html = file_get_contents("http://www.go1235ogle.com/"); //Invalid URL. This line give Error Message
Run Code Online (Sandbox Code Playgroud)
我的代码将输出一条错误消息 Notice: Trying to get property of non-object
如果file_get_contents无法打开给定的URL,我想隐藏它正在输出的错误消息,并用if else语句跳过下面的代码。
if (FALSE === $html)
{
//skip the code in here ?
}
Run Code Online (Sandbox Code Playgroud)
但是上面的代码没有帮助。您能告诉我如何解决此问题吗?
我正在尝试执行以下操作.
当我尝试使用oAuth访问服务时,如果我收到错误,因为我使用了错误的密钥,我想不显示错误消息,而是将用户发送到身份验证页面.
但是使用try catch fn不起作用.
这是我的代码:
$url = 'https://api.soundcloud.com/me.json?'.http_build_query(array(
'oauth_token' => $token,
));
//
try{
$user = json_decode(file_get_contents($url));
return array(
'uid' => $user->id,
'nickname' => $user->username,
'name' => $user->full_name,
'location' => $user->country.' ,'.$user->country,
'description' => $user->description,
'image' => $user->avatar_url,
'urls' => array(
'MySpace' => $user->myspace_name,
'Website' => $user->website,
),
);
}
catch(Services_Soundcloud_Invalid_Http_Response_Code_Exception $e)
{
log_message('error', 'EXCEPTION: '.$e);
return FALSE;
}
Run Code Online (Sandbox Code Playgroud)
我希望这可以返回用户数组或FALSE.
有任何想法吗?