簡體   English   中英

我如何使用 php 從另一個網站獲取 parase 數據

[英]how can i get parase data from another website using php

我想從服務器獲取解碼數據服務器發送方將標題解碼之類的回顯文本發送到我使用的接收方服務器json_decode($json ,tue); $json = file_get_contents($url ,true); 不工作

它在發送服務器中顯示標題,但未進入接收服務器

解析域發件人包括此代碼

<?php
$json = '{
    "title": "PHP"

}';
$data = json_decode($json);
echo $data->title;
?>

來自域發送者的第二個域接收者

<?php

$url = 'https://domain sender';
$json = file_get_contents($url);
$jo = json_decode($json);
echo $jo->title;


if ($jo = 'PHP') {

    $sql = "UPDATE users SET web_status = 'web_int' WHERE website = '".$url."' ";

    if ($conn->query($sql) === true) {
    }
    echo"good";
}else{
    if ($json !== 'PHP') {

        $sql = "UPDATE users SET web_status = 'pendding' WHERE website = '".$url."' ";

        if ($conn->query($sql) === TRUE) {
        }
        echo"bad";
        $conn->close();
    }
}
?>

您的發件人代碼將僅輸出標題值,即“PHP”。

因此,接收方無需嘗試解碼任何 JSON,因為它下載的數據只是一個單詞,采用純文本格式。

我們還可以對接收器代碼進行其他一些簡化和改進,最終得到以下結果:

$url = 'https://domain sender';
$data = file_get_contents($url); //downloaded data is plain text, not JSON
$status = "";

// a simple switch is usually neater than a lot of "if"s
switch ($data) {
  case "PHP":
    $status = "web_int";
    echo "good";
    break;
  default:
    $status = "pending";
    echo "bad";
    break;
}

//always use prepared statements and parameters to execute queries reliably and safely!
$sql = "UPDATE users SET web_status = ? WHERE website = ? ";
$stmt = $conn->prepare($sql);
$stmt->execute(array($status, $url));

在發送服務器上,您有echo $data->title; 哪個回顯/發送'PHP'

然后你有$jo = json_decode($json); echo $jo->title; $jo = json_decode($json); echo $jo->title; 在接收服務器上。 但是->title不是必需的,因為您不發送數組。 變量$jo已經具有值“PHP”。

更新:

改變

$url = 'https://domain sender';
$json = file_get_contents($url);
$jo = json_decode($json);
echo $jo->title;

$url = 'https://domain sender';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

對其進行了測試,並且可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM